Javascript iOS9:如果可能,尝试通过方案打开应用程序,否则重定向到应用程序商店

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32689483/
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-08-23 14:05:29  来源:igfitidea点击:

iOS9: Try to open app via scheme if possible, or redirect to app store otherwise

javascriptiosiframesafariios9

提问by gran33

My question is about iOS9 only!

我的问题是关于iOS9 的!

I have an HTMLlanding page, and I try to redirect the user to my appvia URL scheme if the app is installed, or redirect to the Appstoreotherwise.

我有一个HTML登陆页面,如果安装了应用程序,我会尝试通过 URL 方案将用户重定向到我的应用程序,否则重定向到Appstore

My code is:

我的代码是:

document.addEventListener("DOMContentLoaded", function(event) {

  var body = document.getElementsByTagName('body')[0];
  body.onclick = function () {
    openApp();
  };
});

var timeout;

function preventPopup() {

    clearTimeout(timeout);
    timeout = null;
    window.removeEventListener('pagehide', preventPopup);
}

function openApp(appInstanceId, platform) {

  window.addEventListener('pagehide', preventPopup);
  document.addEventListener('pagehide', preventPopup);

  // create iframe
  var iframe = document.createElement("iframe");
  document.body.appendChild(iframe);
  iframe.setAttribute("style", "display:none;");
  iframe.src = 'myscheme://launch?var=val';

  var timeoutTime = 1000;
  timeout = setTimeout(function () {

    document.location = 'https://itunes.apple.com/app/my-app';

  }, timeoutTime);
}

The problem is that the iframetrick doesn't work in SafariiOS9.

问题是这个iframe技巧在SafariiOS9.

Any idea why?

知道为什么吗?

My iframetrick based on thisanswer.

我的iframe技巧基于这个答案。

回答by st.derrick

The iframe trick no longer works -- my guess is that Apple knows it will encourage more developers to implement Universal Links, more quickly.

iframe 技巧不再有效——我猜苹果知道它会鼓励更多的开发人员更快地实现通用链接。

You can still set window.location='your-uri-scheme://';and fallback to the App Store after 500ms. There is a "dance" between popups if you take this approach, as we do at Branch(we do as a fallback if Universal Links don't work).

您仍然可以window.location='your-uri-scheme://';在 500 毫秒后设置并回退到 App Store。如果您采用这种方法,弹出窗口之间会发生“跳舞”,就像我们在Branch所做的那样(如果通用链接不起作用,我们会作为后备)。

window.location = 'your-uri-scheme://'; // will result in error message if app not installed
setTimeout(function() {
   // Link to the App Store should go here -- only fires if deep link fails                
   window.location = "https://itunes.apple.com/us/app/myapp/id123456789?ls=1&mt=8";
}, 500);

I wish I had a better answer for you. iOS 9 is definitely more limited.

我希望我有一个更好的答案给你。iOS 9 肯定更受限制。

For a helpful overview of what's needed for Universal Links should you go that route, check out my answer hereor read this tutorial

有关通用链接所需内容的有用概述,如果您走那条路线,请在此处查看我的答案或阅读本教程

回答by Bart Teeuwisse

As already mentioned setting window.locationon iOS 9 still works. However, this brings up an Open in App dialog. I've put an example on https://bartt.me/openappthat:

如前所述window.location,iOS 9 上的设置仍然有效。但是,这会打开一个在应用程序中打开的对话框。我在https://bartt.me/openapp上举了一个例子:

  1. Launches Twitter when the Open in Twitter appis clicked.
  2. Falls back to the Twitter app in the App Store.
  3. Redirects to Twitter or the App Store withoutthe user selecting Openin the Open in Appdialog.
  4. Works in all browsers on iOS and Android.
  1. 单击“在 Twitter 中打开”应用程序时启动 Twitter 。
  2. 回到 App Store 中的 Twitter 应用程序。
  3. 重定向到 Twitter 或 App Store,而无需用户在“在应用程序打开”对话框中选择“打开” 。
  4. 适用于 iOS 和 Android 上的所有浏览器。

Look at the source of https://lab.bartt.me/openappfor more information.

查看https://lab.bartt.me/openapp的来源了解更多信息。

回答by carlos.baeza

Maybe try giving you app support to Universal Links

也许尝试为您提供对通用链接的应用程序支持

Idea: Avoid custom (JavaScript, iframe) solutions in Safari, replace you code with a supported Universal Link.

想法:避免在 Safari 中使用自定义(JavaScript、iframe)解决方案,用受支持的通用链接替换您的代码。

Example

例子

<html>
<head>
...
</head>
<body>
    <div class"app-banner-style">
        <a href="http://yourdomain.com">In app open</a> 
    </div>
...content
</body>
</html>

if you app support Universal Links (e.g. yourdomain.com), you muss configure your domain (and path) and iOS9 should be react to it link opening you App. That is only theory, but I guess should be work :)

如果您的应用程序支持通用链接(例如 yourdomain.com),您必须配置您的域(和路径),iOS9 应该对它的链接做出反应,打开您的应用程序。这只是理论,但我想应该是可行的:)

https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/AppSearch/UniversalLinks.html#//apple_ref/doc/uid/TP40016308-CH12

https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/AppSearch/UniversalLinks.html#//apple_ref/doc/uid/TP40016308-CH12

回答by xac

iframe hack doesn't work in ios9 anymore. Possible solution is use two buttons. Example:

iframe hack 不再适用于 ios9。可能的解决方案是使用两个按钮。例子:

$('#goToStoreBtn').text( "go to store" ).click(function(event){
    event.preventDefault();
    event.stopPropagation();
    window.location = storeUrl; // https://itunes.apple.com/...
});

$('#goToAppBtn').text( "go to app" ).click(function(event){
    event.preventDefault();
    event.stopPropagation();
    window.location = appUrl;  // myApp://...
});

回答by prabeen giri

This is relatively old thread, but I have created a library that supports deeplinking on most of the modern mobile browsers. But this requires separate deeplinking page which needs to be hosted in different domain to support universal linking in ios9 facebook browser.

这是一个相对较旧的线程,但我创建了一个支持大多数现代移动浏览器上的深度链接的库。但这需要单独的深层链接页面,该页面需要托管在不同的域中,以支持 ios9 facebook 浏览器中的通用链接。

https://github.com/prabeengiri/DeepLinkingToNativeApp

https://github.com/prabeengiri/DeepLinkingToNativeApp