sebestindragos / nativescript-plugin-universal-links

Universal links (IOS) and App Links (Android) support for NativeScript.
8 stars 7 forks source link

registerUniversalLinkCallback not being called #3

Closed dimitriospafos closed 4 years ago

dimitriospafos commented 4 years ago

I added the AASA file to my root dir and validated it which looks good. I added the associated domain via Xcode.

The app launches correctly when i tap on a link, but registerUniversalLinkCallback is not being invoked. I tried setting a breakpoint on the console.log line below in the app.component.ts and it never gets hit and I don't see any logs in the output

package.json:

.....
"tns-ios": {
      "version": "6.1.1"
    }
  },
  "dependencies": {
    "@angular/animations": "~8.0.0",
    "@angular/common": "~8.0.0",
    "@angular/compiler": "~8.0.0",
    "@angular/core": "~8.0.0",
    "@angular/forms": "~8.0.0",
    "@angular/http": "8.0.0-beta.10",
    "@angular/platform-browser": "~8.0.0",
    "@angular/platform-browser-dynamic": "~8.0.0",
    "@angular/router": "~8.0.0",
    "@nstudio/nativescript-checkbox": "^1.0.0",
    "@nstudio/nativescript-loading-indicator": "^1.0.0",
    "@proplugins/nativescript-cfalert-dialog": "^2.0.1",
    "@proplugins/nativescript-drop-down": "^7.1.0",
    "@proplugins/nativescript-keyboardshowing": "^2.1.0",
    "base-64": "^0.1.0",
    "moment": "^2.19.1",
    "nativescript-angular": "8.0.2",
    "nativescript-background-http": "^3.3.0",
    "nativescript-barcodescanner": "^3.1.3",
    "nativescript-bitmap-factory": "^1.8.1",
    "nativescript-bluetooth": "^2.0.0-beta.27",
    "nativescript-blur": "^2.0.0",
    "nativescript-directions": "^1.1.2",
    "nativescript-drop-down": "^3.2.5",
    "nativescript-fancyalert": "^3.0.6",
    "nativescript-feedback": "^1.2.0",
    "nativescript-fingerprint-auth": "^6.2.0",
    "nativescript-geolocation": "^5.1.0",
    "nativescript-google-maps-sdk": "^2.8.1",
    "nativescript-imagecropper": "^1.0.4",
    "nativescript-imagepicker": "^7.0.0",
    "nativescript-iqkeyboardmanager": "^1.5.1",
    "nativescript-local-notifications": "^4.0.1",
    "nativescript-location": "^0.1.3",
    "nativescript-modal-datetimepicker": "^1.2.2",
    "nativescript-ng-ripple": "^2.0.1",
    "nativescript-ngx-fonticon": "^4.2.0",
    "nativescript-oauth": "^2.0.1",
    "nativescript-permissions": "^1.3.7",
    "nativescript-plugin-universal-links": "^1.0.1",
    "nativescript-secure-storage": "^2.3.0",
    "nativescript-ssl-pinning": "^1.1.4",
    "nativescript-theme-core": "^1.0.4",
    "nativescript-ui-listview": "^8.0.1",
    "nativescript-ui-sidedrawer": "^8.0.1",
    "nativescript-websockets": "^1.3.4",
    "reflect-metadata": "~0.1.8",
    "rxjs": "^6.3.3",
    "rxjs-compat": "^6.3.2",
    "tns-core-modules": "~6.1.0",
    "zone.js": "^0.9.1"
  },
  "devDependencies": {
    "@angular/compiler-cli": "8.0.0",
    "@ngtools/webpack": "8.0.0",
    "nativescript-dev-webpack": "~1.2.0",
    "tns-platform-declarations": "^6.0.1",
    "typescript": "~3.4.5"
  }
}

entitlements file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.developer.associated-domains</key>
    <array>
        <string>applinks:www.mywebsite.com</string>
    </array>
</dict>
</plist>

app.component.ts :

registerUniversalLinkCallback(ul => {

        //never gets called
        console.log("CALLED UNIVERSAL LINK")
    });
sebestindragos commented 4 years ago

Yeah, I was afraid something like this might happend, but could never reproduce in my own project. Here's what you could try: go edit the code in

node_modules/nativescript-plugin-universal-links/build/plugin-universal-links.common.js

Inside the registerUniversalLinkCallback function body, put this code:

function registerUniversalLinkCallback(cb) {
  callback = cb; // this was already there

  if (universalLink) cb(universalLink); // add this
}

Rebuild your project and test. I think this could happen if the universal link gets parsed before the callback is registered.

dimitriospafos commented 4 years ago

@sebestindragos the above line of code does not seem to make any difference for me. registerUniversalLinkCallback is not being invoked even though the app opens.

I also tried to check "ul" in below code after the app opens from clicking on a link but I get undefined back. const ul = getUniversalLink();

Is there anything else that can be causing this? My iphone is on 13.4.1 btw

sebestindragos commented 4 years ago

@dimitriospafos I think one of your other plugins might be replacing the application delegate. Could you make sure the universal link plugin is imported/required last? It has support for working with an existing delegate.

dimitriospafos commented 4 years ago

@sebestindragos I have a custom ios delegate assigned in main.ts which I guess overwrites the delegate that your plugin is initializing. I tried removing my custom delegate which fixed the above issue.

My custom delegate assignment in main.ts:

import * as application from "tns-core-modules/application";
...

class BackgroundAppDelegate extends UIResponder implements UIApplicationDelegate {
        public static ObjCProtocols = [UIApplicationDelegate];

        public applicationPerformFetchWithCompletionHandler(application: UIApplication, completionHandler: any) {
...
        }
        public applicationDidFinishLaunchingWithOptions(application: UIApplication, launchOptions: NSDictionary<string, any>): boolean {
            …
        }
    }

    application.ios.delegate = BackgroundAppDelegate;

The idea of the custom delegate is from: https://docs.nativescript.org/core-concepts/ios-runtime/how-to/BackgroundExecution

How can I make this work with your plugin? The custom delegate needs to be assigned before your plugin initializes the delegate, but I can't think of a way to do this at the moment. Any help would be appreciated here. Thanks

sebestindragos commented 4 years ago

@dimitriospafos Try extracting your custom delegate in a separate file and in main.ts first you load that and then the universal links plugin

import './myBackgroundRunDelegate';
import {} from 'nativescript-plugin-universal-links';
dimitriospafos commented 4 years ago

@sebestindragos Tried that, doesn't work. I think the issue is that when I assign the custom background delegate like this : "application.ios.delegate = BackgroundAppDelegate; " after the imports it will overwrite the one from your plugin.

sebestindragos commented 4 years ago

@dimitriospafos you're doing it wrong. Put your entire code in that file, even the assignment

// myBackgroundRunDelegate.ts
import * as application from "tns-core-modules/application";
...
class BackgroundAppDelegate extends UIResponder implements UIApplicationDelegate {
}

application.ios.delegate = BackgroundAppDelegate; // the assignment is done here, not in main.ts

and in main.ts

// this will import the file which will assign the background delegate
// notice there is no import * as bgDeletegate, just import /file/path
import './myBackgroundRunDelegate';

// this will load the universal plugin which will overwrite your custom delegate
import { registerUniversalLinkCallback } from 'nativescript-plugin-universal-links';

// do something with the universal link
registerUniversalLinkCallback(ul => {
  ...
});
dimitriospafos commented 4 years ago

@sebestindragos I tried the above still didn't work. Only it works is if I don't assign my custom delegate. I'm guessing the import statements might not be in order? At this point I'll rather implement the ios event: applicationContinueUserActivityRestorationHandler?(application: UIApplication, userActivity: NSUserActivity, restorationHandler: (p1: NSArray<UIUserActivityRestoring>) => void): boolean; myself without using the plugin. I appreciate taking time to help me here. Thank you.

sebestindragos commented 4 years ago

@dimitriospafos Well, feel free to use the source code from the plugin. You'll have to also do it for Android.