Nativescript: Get deeplink URL in iOS?

Created on 13 Feb 2017  路  7Comments  路  Source: NativeScript/NativeScript

_From @zahid-dapperapps on February 13, 2017 13:39_

Documentation has code for android that is working and I am able to get value of what is being passed.

application.android.on(application.AndroidApplication.activityResumedEvent, (args) => {
    var intentData = args.activity.getIntent().getData();
});

How can I do the same for iOS? documentation has code for iOS for resume event but it doesn't really show how to get whats being passed via deeplink url

application.on(application.resumeEvent, function (args: application.ApplicationEventData) {
    if (args.android) {
        console.log("Activity:================= " + args.android);
    } else if (args.ios) {
        console.log("UIApplication:============== " + args.ios);
    }
});

_Copied from original issue: NativeScript/ios-runtime#727_

ios

Most helpful comment

you could take a look at my plugin: https://www.npmjs.com/package/nativescript-urlhandler

All 7 comments

Hi @zahid-dapperapps,
To be able to use a deep link in iOS, you should make some changes in your project Info.plist and implement applicationHandleOpenURL delegate method, which allows you to get the URL.
You could review the below-attached description, how you could do that in your app.

The first step is to include CFBundleURLName key in your app/App_Resources/iOS/Info.plist file.
For example:

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string><your app id></string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>appgo</string>
                <string>yourSecondScheme</string>
            </array>
        </dict>
    </array>

<your app id> could get it from the project package.json file and should look like the following string org.nativescript.issue3632.

To be able to implement UIApplicationDelegate and its applicationHandleOpenURL method, you should install tns-platform-declarations with npm install tns-platform-declarations --save and to follow setup instructions in npm here.

The final steps is to implement the needed method in app.ts file for TypeScript project and app.js file for pure JavaScript template:

import "./bundle-config";
import * as app from 'application';
var application = require("application");





application.on(app.resumeEvent, function (args) {
    if (args.android) {
        console.log("Activity: " + args.android);
    }
});


function getParams(url){
    console.log(url);
    var resulturl:string = (<any>NSString)(url).toString();;
    if(resulturl.substring(0,5)=="appgo"){
       console.log(getParameterByName("test", resulturl));
    }

}

function getParameterByName(name, url) {
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return results[2].replace(/\+/g, " ");
}

class newIOSApplication extends NSObject implements UIApplicationDelegate{
static ObjCProtocols = [UIApplicationDelegate];
        applicationHandleOpenURL(app, url): boolean {
            getParams(url);
            return true;
        }
}
application.ios.delegate = newIOSApplication;

app.start({ moduleName: 'main-page' });

to be able to open the app with some parameters you could use URL similar to appgo://org.nativescript.issue3632?test=test and getParameterByName allows you to get single parameter.

Hope this information helps.

Hi @zahid-dapperapps,
This feature seems to be ideal candidate for a separate plugin, which extend this functionality for both iOS and Android. For further help, you could review the below-attached articles, where has been described how to create a plugin.

https://docs.nativescript.org/plugins/plugins
http://developer.telerik.com/featured/building-your-own-nativescript-modules-for-npm/
http://developer.telerik.com/featured/write-nativescript-plugins-theyre-easier-cordova-plugins/
http://developer.telerik.com/featured/creating-nativescript-plugins-in-typescript/
http://fluentreports.com/blog/?p=167

Regards,
@tsonevn

you could take a look at my plugin: https://www.npmjs.com/package/nativescript-urlhandler

@hypery2k your packaged worked for me in Android deeplink. thanks!

@hypery2k For me its not coming inisde the handleOpenUrl method. I follow all the steps that you provided

@preethijayanthi can you try using applicationOpenURLOptions? => https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623112-application?language=objc
Let me know 馃憤

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings