iOS:如果已安装,则使用自定义 URL 打开应用程序,否则使用 iTunes

iOS: opening app with custom URL if installed, itunes otherwise

我为我的网站实施了 smart app banner,以便将用户重定向到我们的 Android 或 iOS 应用程序。通过解析用户代理,我可以成功重定向到 Android 应用程序或 Google Play 商店(使用意图过滤器):现在我想对 iOS 做同样的事情,打开应用程序如果它已安装并在别处打开 iTunes。

我设法打开应用程序,如果它已安装,使用自定义 URL 方案

<a href="myurlscheme://main">

现在,如果设备上未安装该应用程序,我想让 iTunes 启动。我怎样才能做到这一点?

谢谢

试试这个 <meta"apple-itunes-app"content"app-id=myAppStoreID, affiliate-data=myAffiliateData, app-argument=myURL"

<a href="xxx://www.xxx.com">Open APP</a>

老实说,这对您自己实施来说是一种痛苦。在检测到 iOS 用户代理后,您可以在服务器上实现 JavaScript 重定向,如下所示:

setTimeout(function() {
  window.location = "https://itunes.apple.com/path/to/your/app/";
}, 25);

// If "yourapp://" is registered, the user will see a dialog
// asking if want to open your app. If they agree, your app will 
// launch  immediately and the timer won't fire.
// If not installed, you'll get an ugly "Cannot Open Page" 
// dialogue and the App Store will launch when the timer expires.

window.location = "yourapp://";

显然这不是一个理想的解决方案,它有大量令人讨厌的边缘情况,最显着的是如果用户没有您的应用程序,他们将在被重定向到 App Store 之前看到“无法打开页面”错误安装。直到最近,才有可能通过使用此脚本的更细微版本以合理用户友好的方式解决此问题。可悲的是,Apple intentionally broke that iOS 9.2 更新。

你也可以enable Universal Links。 Apple 知道这是一个恼人的问题,并正在努力提供帮助。 Universal Links 让您可以使用普通的 URL 到您网站上的页面(这可能是一个简单的重定向到 App Store 而没有 自定义 URL 触发'Cannot Open Page' 错误),它会被您的 phone 拦截并直接发送到您的应用程序(如果已安装)。不幸的是,通用链接仅适用于 iOS 9+,并且在许多应用程序中打开时尚不可用。

最好的解决方案是结合上述方法:通用链接和应用链接在所有地方都受支持,智能 JavaScript 重定向作为后备。这需要处理的事情很多,因此最好的选择可能是像 Branch.io 这样的免费服务(完全披露:我与团队合作)来处理所有技术方面的问题。