Universal links (IOS) and App Links (Android) support for NativeScript.
When a user clicks a link to a website, it opens in the default web browser (Safari/Chrome). Universal linking allows your app to open instead of the web browser.
Apple calls this Universal Links and Google calls it App Links, but they mean the same thing.
![]() | ![]() |
---|---|
iOS Demo | Android Demo |
In version 3.0.0 the returned object is now simply the link as a string
. It is not "parsed" anymore. The reason for that is that url-parse
package used for this is pretty huge and not needed by most.
You can still parse like so:
import * as urlparse from 'url-parse';
function parseLink(link: string) {
const url = urlparse(link, true);
return {
href: url.href,
origin: url.origin
pathname: url.pathname,
query: url.query
}
}
Run the following command from the root of your project:
ns plugin add @nativescript-community/universal-links
Both iOS (9.0 and newer) and Android (all versions) provide good APIs for universal linking.
Apple introduced a new deep linking API in iOS 9.0 called “Universal Links”. It provides a better user experience than the hacky deep linking options that existed in iOS 8.0 and below.
First step is to add a file to the root of your website called apple-app-site-association
. This is a JSON file and it looks like this:
{
"applinks": {
"apps": [],
"details": [
{
"appID": "TEAM_ID.BUNDLE_ID", // ex: "9JA89QQLNQ.com.apple.wwdc"
"paths": [ "/blog/*"]
}
]
}
}
apple-app-site-association
is correct.Check out Apples' docs for more info.
Next, you need to add the Associated Domains to your IOS project, either using XCode or manually adding the following code to your App_Resources/IOS/app.entitlements
file. Please note the applinks:
prefix, it won't work without it.
<?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.example.com</string>
</array>
</dict>
</plist>
In Android, universal linking is implemented using Intent Filters. By adding a BROWSABLE intent filter, you are saying that your app can be started by a user clicking on a website url.
You don't need any server side changes for Android, only modify your app to add the Intent Filter.
Add this code to your App_Resources/Android/src/main/AndroidManifest.xml
file:
<activity
android:name="com.tns.NativeScriptActivity"
android:label="@string/title_activity_kimera" >
<!-- Add this new section to your Activity -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Handle urls starting with "https://www.example.com/blog" -->
<data android:scheme="https"
android:host="www.example.com"
android:pathPrefix="/blog" />
</intent-filter>
</activity>
Call the registerUniversalLinkCallback
somewhere in the startup of your app. This Angular example puts it in the AppComponent's ngOnInit method to provide a callback method which will receive an Universal Link object every time your app is opened by a website link:
import { Component, OnInit } from "@angular/core";
import { registerUniversalLinkCallback } from "@nativescript-community/universal-links";
@Component({
selector: "my-app",
template: "<page-router-outlet></page-router-outlet>"
})
export class AppComponent {
constructor() {}
ngOnInit() {
registerUniversalLinkCallback(ul => {
// use the router to navigate to the screen
});
}
}
The universal link object has the following structure:
{
"href": "https://www.example.com/blog?title=welcome",
"origin": "https://www.example.com",
"pathname": "/blog",
"query": "?title=welcome"
}
There is also a getUniversalLink()
method that will return the last universal link which opened the app. This is useful in scenarios where your app is protected by a login screen. Check if the user is authenticated and then navigate to the desired path.
import { getUniversalLink } from "nativescript-plugin-universal-links";
const ul = getUniversalLink();
To run the demos, you must clone this repo recursively.
git clone https://github.com/@nativescript-community/universal-links.git --recursive
Install Dependencies:
npm i # or 'yarn install' or 'pnpm install'
Interactive Menu:
To start the interactive menu, run npm start
(or yarn start
or pnpm start
). This will list all of the commonly used scripts.
npm run build
npm run build.angular # or for Angular
npm run demo.[ng|react|svelte|vue].[ios|android]
npm run demo.svelte.ios # Example
If you have any questions/issues/comments please feel free to create an issue or start a conversation in the NativeScript Community Discord.
Version | Tag | Published |
---|---|---|
3.0.0 | latest | 7d ago |