javascript 如果通过 Safari 中的网页安装,如何打开应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17618254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:59:02  来源:igfitidea点击:

How to open an app if installed through a webpage in Safari?

javascripthtmliosobjective-c

提问by Amogh Talpallikar

We have an app, lets call it: xyz_appfor which I have a custom URL scheme in form of xyz_app://.

我们有一个应用程序,我们称之为:xyz_app我有一个自定义 URL 方案,格式为xyz_app://.

We are sending an email to the user.

我们正在向用户发送电子邮件。

when he clicks on the link, he is suppose to go to the app and in case the app is not installed, our mobile website.

当他点击链接时,他应该会转到该应用程序,如果未安装该应用程序,则会访问我们的移动网站。

so from the Link in the email, how do I make sure that if the app is not installed, that link should send the user to the mobile website instead ?

因此,从电子邮件中的链接,我如何确保如果未安装该应用程序,该链接应将用户发送到移动网站?

回答by vforvendetta

The URL scheme works directly only if the app is installed on the iDevice. If not installed then it will throw an error.

仅当应用程序安装在 iDevice 上时,URL 方案才能直接工作。如果未安装,则会引发错误。

1) To implement this functionality you will have to redirect the user to a webpage that will detect if app is installed, from your email link. Something like this www.yourwebsite/detectapp

1) 要实现此功能,您必须将用户重定向到一个网页,该网页将通过您的电子邮件链接检测是否安装了应用程序。像这样的东西 www.yourwebsite/detectapp

2) Your detectapp page will have a javascript-

2)您的detectapp页面将有一个javascript-

var appstoreFail = "www.your_redirect_website.com";

//Check is device is iOS
var iOS = /(iPad|iPhone|iPod)/.test(navigator.userAgent);
var appUrlScheme = "xyz_app://";

if (iOS) {
    // If the app is not installed the script will wait for 2sec and redirect to web.
    var loadedAt = +new Date;
    setTimeout(function() {
        if (+new Date - loadedAt < 2000)
            window.location = appstoreFail;
    } ,25);
    // Try launching the app using URL schemes
    window.open(appUrlScheme, "_self");
} else {
    // Launch the website
    window.location = appstoreFail;
}