Last active
July 12, 2022 03:41
-
-
Save avneeshroks/c210c7110bf1f9bee854df31de281371 to your computer and use it in GitHub Desktop.
To Have a universal link setup for android and ios device open app if there else open app store
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var getSegment = function (url, index) { | |
return url.replace(/^https?:\/\//, '').split('/')[index]; | |
} | |
var App = function(options) { | |
this.init(options); | |
}; | |
App.prototype = { | |
init : function(options) { | |
//Set options in property | |
this.options = options; | |
//Detect device | |
this.device = this.checkDevice(); | |
// Set username | |
this.username = getSegment(window.location.pathname, 2)?getSegment(window.location.pathname, 2):'' ; | |
// Set appUrl and replaceUrl based on device recognised | |
this.setAppAndReplaceUrl(this.username); | |
this.launchApp(); | |
}, | |
launchApp : function() { | |
console.log(this.appUrl); | |
if(this.device != "WEB") { | |
window.location.replace(this.appUrl); | |
var that = this; | |
setTimeout(function(){ | |
that.openWebApp(); | |
}, 2000); | |
} | |
}, | |
openWebApp : function() { | |
var askForStore = confirm("Don't have app? get from store"); | |
if (askForStore == true) { | |
window.location.replace(this.replaceUrl); | |
} | |
}, | |
checkDevice : function() { | |
if (/iPhone/i.test(navigator.userAgent)) { | |
return "IOS"; | |
} else if (/Android/i.test(navigator.userAgent)) { | |
return "ANDROID"; | |
} else { | |
return "WEB" | |
} | |
}, | |
setAppAndReplaceUrl : function(username) { | |
switch(this.device) { | |
case 'IOS': | |
this.appUrl = this.options.ios.appUrlSchema + username; | |
this.replaceUrl = this.options.ios.replaceUrl; | |
break; | |
case 'ANDROID': | |
this.appUrl = this.options.android.appUrlSchema + username; | |
this.replaceUrl = this.options.android.replaceUrl; | |
break; | |
default: | |
this.appUrl = document.URL; | |
break; | |
} | |
} | |
} | |
var app = new App({ | |
android : { | |
appUrlSchema : "myapp://", | |
replaceUrl : "https://play.google.com/store/apps/details?id=com.myapp&hl=en" | |
}, | |
ios : { | |
appUrlSchema : "myapp:/", | |
replaceUrl : "https://itunes.apple.com/in/app/myapp-discovery-app-for/id0001?mt=8" | |
}, | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment