iOS 9 带有自定义 url 方案的 safari iframe src 不工作

iOS 9 safari iframe src with custom url scheme not working

如果安装了应用程序,我会使用此解决方案 https://gist.github.com/davidwkeith/2662899 从网页重定向到我的应用程序。 但它在 iOS 9 上不起作用。它仍然在 Google-chrome 上起作用。但是 iframe 使用自定义 URL 方案无法从 Safari 启动应用程序。

如果我替换

document.getElementById('loader').src = 'custom-protocol://my-app'

(加载程序是 iframe)和

window.location = 'custom-protocol://my-app'

它会起作用。

os: iOS 9 beta4 和 beta5

有人知道这个问题吗?是 iOS 9 beta 错误吗?还是不修复?

是的,iOS9 现在你可以深入 link。查看 link 以获得详细说明,但我列出了基础知识。

http://blog.hokolinks.com/how-to-implement-apple-universal-links-on-ios-9/

首先您必须转到您的目标并单击功能。添加关联域。

接下来您必须上传 apple-app-site-association 文件。

基本上打开一个 JSON 编辑器并构建类似这样的东西

{
  "applinks": {
"apps": [],
"details": {
  "TBEJCS6FFP.com.domain.App": {
    "paths":[ "*" ]
  }
}
  }
}

接下来,您必须在您的应用中支持通用 links。您需要实施

extension AppDelegate {
func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
    if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
        let webpageURL = userActivity.webpageURL! // Always exists
        if !handleUniversalLink(URL: webpageURL) {
            UIApplication.sharedApplication().openURL(webpageURL)
        }
    }
    return true
}

private func handleUniversalLink(URL url: NSURL) -> Bool {
    if let components = NSURLComponents(URL: url, resolvingAgainstBaseURL: true), let host = components.host, let pathComponents = components.path?.pathComponents {
        switch host {
        case "domain.com":
            if pathComponents.count >= 4 {
                switch (pathComponents[0], pathComponents[1], pathComponents[2], pathComponents[3]) {
                case ("/", "path", "to", let something):
                    if validateSomething(something) {
                        presentSomethingViewController(something)
                        return true
                    }
                default:
                    return false
                }

之前的答案是通用链接的部分实现,缺少关键细节并且不包括对 App Store 的回退。

首先,您不能再设置 iframe src 来触发 URI 方案。您已正确识别该问题。正如您所指出的,您仍然可以设置 window.location = 'custom-protocol://my-app';。因此,如果您知道用户拥有您的应用程序是因为您之前从浏览器打开了他们的应用程序并且存储了一个可以在您的后端查找的 cookie,您仍然可以安全地启动 custom-protocol://.

其次,您可以使用 navigator.userAgent 检测用户代理字符串。 Pre-iOS 9 您仍然可以使用 iframe 触发 URI 方案,然后在超时后回退。在 iOS 9 上,您可以选择是否根据 cookie 触发 URI 方案,然后将用户带到 App Store。我在 Branch 从事此工作,并使用 cookie 来回忆用户是否可能拥有我们已经实现的应用程序。如果您对此有更多疑问,请随时联系我们,或直接使用我们的解决方案。


实施通用链接并不像其他答案描述的那么简单。实际上,复杂性要高得多。以下是完整的步骤列表(我在最近几周使用这些步骤帮助集成了多个应用程序):

1。配置您的应用以注册批准的域

我。如果您还没有

,请在 developer.apple.com 注册您的应用

二。在 developer.apple.com

上为您的应用程序标识符启用“关联域”

三。在您的 Xcode 项目

中启用“关联域”

四。在您的应用程序

中添加适当的域授权 applinks:yourdomain.com

2。配置您的网站以托管“apple-app-site-association”文件

我。购买域名或从现有

中选择

二。为域名获取SSL认证(可以使用CloudFlare!)

三。创建结构化的“apple-app-site-association”JSON 文件

{
   "applinks": {
       "apps": [ ],
       "details": {
           "TEAM-IDENTIFIER.YOUR.BUNDLE.IDENTIFIER": {
               "paths": [
                   "*"
               ]
           }
       }
   }
}

四。使用 SSL 证书签署 JSON 文件


v。配置文件服务器

apple-app-site-association 文件: - 必须与 header ‘application/pkcs7-mime’ 一起发送 - 必须从端点 youdomain 发送。com/apple-app-site-association - 必须 return 200 http 代码。

示例快递+节点:

var aasa = fs.readFileSync(__dirname + '/static/apple-app-site-association');
app.get('/apple-app-site-association', function(req, res, next) {
     res.set('Content-Type', 'application/pkcs7-mime');
     res.status(200).send(aasa);
});

信用:大量借自this blog post