nordnet / cordova-universal-links-plugin

[DEPRECATED] - Cordova plugin to support Universal/Deep Links for iOS/Android.
https://github.com/nordnet/cordova-universal-links-plugin/issues/160
MIT License
349 stars 529 forks source link

Remote app scenario #72

Closed GioviQ closed 8 years ago

GioviQ commented 8 years ago

My index.js is

var app = {
    initialize: function () {
        this.bindEvents();
    },
    bindEvents: function () {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    onDeviceReady: function () {
        universalLinks.subscribe(null, app.onRemotePageRequested);

        window.location.replace("https://www.example.com")
    },
    onRemotePageRequested: function (eventData) {
        window.location.replace(eventData.url);
    }
 };

 app.initialize();

My cordova app is remote so window.location.replace redirect to remote site. When app is launched onDeviceReady executes before onRemotePageRequested (ok?) so do I always miss the universalLinks event, because www.example.com is currently loading / loaded? Thanks

GioviQ commented 8 years ago

Maybe when onDeviceReady is executed I should just know if the app is started because a link was clicked or not.

nikDemyankov commented 8 years ago

If I'm not mistaking, when app is read - you immediately redirect it to example.com. Because of that onRemotePageRequested is not gonna be called: web page is different almost immediately after it's ready.

As a workaround you can try and add some timeout for redirecting to example.com:

var app = {
    initialize: function () {
        this.bindEvents();
    },
    bindEvents: function () {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    onDeviceReady: function () {
        universalLinks.subscribe(null, app.onRemotePageRequested);

        app.redirectTimeoutID = setTimeout(function() {
            window.location.replace("https://www.example.com");
        }, 2000);
    },
    onRemotePageRequested: function (eventData) {
        clearTimeout(app.redirectTimeoutID);
        window.location.replace(eventData.url);
    }
 };

 app.initialize();

universalLinks.subscribe(null, app.onRemotePageRequested) sends request to the plugin to get notified, when default UL event occur. If app was launched from the link - onRemotePageRequested will be called almost immediately. If not - when app was launched from the icon on the device. By setting timeout for redirect you give plugin some time to respond. 2sec might be too much, most likely it will take few milliseconds, but you will have to experiment with that.

GioviQ commented 8 years ago

thanks, for now I have seen it is called immediately