Skip to content

Instantly share code, notes, and snippets.

@EddyVerbruggen
Last active January 19, 2022 02:55
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EddyVerbruggen/cf713b9f860119913364b06d880f5b25 to your computer and use it in GitHub Desktop.
Save EddyVerbruggen/cf713b9f860119913364b06d880f5b25 to your computer and use it in GitHub Desktop.
App links on iOS and Android with NativeScript
/*
Add this to your <activity>, so any link (clicked in fi. an e-mail)
will open your app instead of the website, but only if it matches these whitelisted path patterns
*/
<activity android:launchMode="singleInstance"><!-- set the launchMode property to this value! -->
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="https" android:host="*.mywebsite.com" android:pathPattern="/action_planner"/>
<data android:scheme="https" android:host="*.mywebsite.com" android:pathPattern="/.*/action_planner"/>
</intent-filter>
</activity>
/*
You'll need to 'require' the previous file and set it as the AppDelegate like so:
*/
import * as application from "tns-core-modules/application";
if (application.ios) {
application.ios.delegate = require("./ios-app-delegate-extensions").IOSAppDelegateExtensions;
}
/*
For iOS you need to create a file 'apple-app-site-association' and save it to the root of the https(!) website
you want to steal links off. The file needs to be served as json, much like this example: https://app.mydailylifestyle.com/apple-app-site-association
Note that 'YAZ6LJG875' below is the team id or app id.
*/
{
"applinks": {
"apps": [],
"details": [
{
"appID": "YAZ6LJG871.com.your.PackageName",
"paths": [
"/action_planner",
"/*/action_planner"
]
},
{
"appID": "YAZ6LJG871.com.your.OtherPackageName",
"paths": [
"/action_planner",
"/*/action_planner"
]
}
]
}
}
/*
Add this file to yout /app folder.
*/
import { NSAppService } from "~/mobile/core";
export class IOSAppDelegateExtensions extends UIResponder implements UIApplicationDelegate {
//noinspection JSUnusedGlobalSymbols
public static ObjCProtocols = [UIApplicationDelegate];
private static _callback: Function = null;
public static setCallback(callback?: Function) {
this._callback = callback;
}
// iOS >= 9.1
public applicationContinueUserActivityRestorationHandler(application: UIApplication, userActivity: NSUserActivity, restorationHandler: (p1: NSArray<any>) => void): boolean {
if (userActivity.webpageURL) {
console.log("applicationContinueUserActivityRestorationHandler, userActivity.webpageURL: " + userActivity.webpageURL);
// save the url somewhere - I'm simply using a static property in some class.
NSAppService.openAppFromWebUri = userActivity.webpageURL.absoluteString;
return true;
}
return false;
}
}
/*
While bootstrapping you app, you can see if that delegate method set the 'NSAppService.openAppFromWebUri' property.
Note that this structure really depends on what your app looks like. For instance, you could throw this code in the
constructor of app.module.ts
*/
import * as nsApp from "tns-core-modules/application";
nsApp.on(nsApp.resumeEvent, (eventData: nsApp.ApplicationEventData) => {
// on cold boot, this runs before the delegate method which sets NSAppService.openAppFromWebUri, so using a timeout
setTimeout(() => {
if (NSAppService.openAppFromWebUri) {
// This is app-specific code, where I'm checking a user profile for permissions and logged-in state.
const profile: Profile = storage.getItem(StorageKey.PROFILE);
if (profile === null) {
// not logged in, so let's handle the link after login
// Note that the after-login code is not included in this Gist, but it simply calls 'handleUniversalLink' after a little timeout.
return;
}
handleUniversalLink(this.routerExtensions, profile);
}
}, 100); // This is suboptimal.. better use a native event, but didn't bother to find one yet as it seems to work just fine
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment