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

时间:2013-07-12 15:15:00

标签: javascript html ios objective-c

我们有一个应用,我们可以调用它:xyz_app我有xyz_app://形式的自定义网址方案。

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

当他点击链接时,他想要去应用程序,如果没有安装应用程序,我们的移动网站。

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

1 个答案:

答案 0 :(得分:25)

只有在iDevice上安装了应用程序时,URL方案才能直接运行。如果没有安装,则会抛出错误。

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

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;
}