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

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

iOS 9 safari iframe src with custom url scheme not working

iossafariios9

提问by Kirill

I use this solution https://gist.github.com/davidwkeith/2662899to redirect from web page into my app if app installed. But it doesn't work on iOS 9. It's still working in Google-chrome. But iframewith custom URL scheme can't launch the application from Safari.

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

If I replace

如果我更换

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

(where loader is iframe) with

(其中加载程序是 iframe)与

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

it will work.

它会起作用。

os: iOS 9 beta4 and beta5

操作系统:iOS 9 beta4 和 beta5

Anybody knows this problem? Is it iOS 9 beta bug? Or it will not be fixed?

有人知道这个问题吗?这是 iOS 9 测试版的错误吗?或者它不会被修复?

采纳答案by st.derrick

The previous answer is a partial implementation of Universal Links that is missing critical details and doesn't include a fallback to the App Store.

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

First, you can no longer set iframe src in order to trigger a URI scheme. You've correctly identified that issue. As you noted, you can, however, still set window.location = 'custom-protocol://my-app';. So if you know that a user has your app because you've previously opened their app from the browser and have a cookie stored that can be looked up on your backend, you can still safely fire custom-protocol://.

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

Second, you can detect the user agent string using navigator.userAgent. Pre-iOS 9 you can still use the iframe to fire a URI scheme, then fallback after a timeout. On iOS 9, you can choose whether to fire the URI scheme or not based on cookies, then take the user to the App Store. I work on this at Branchand making use of cookies to recall whether a user likely has the app is something we've implemented. Feel free to reach out if you have more questions about that, or make use of our solution directly.

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



Implementing Universal Linksis not quite as simple as the other answer describes. In reality, there is considerably more complexity. Here's a complete list of steps (I've helped several apps integrate in recent weeks using these steps):

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

1. Configure your app to register approved domains

1. 配置您的应用程序以注册批准的域

i. Registered your app at developer.apple.com if you haven't

一世。如果您尚未在 developer.apple.com 上注册您的应用

ii. Enable ‘Associated Domains' on your app identifier on developer.apple.com

ii. 在 developer.apple.com 上为您的应用标识符启用“关联域”

iii. Enable ‘Associated Domain' on in your Xcode project

三、在 Xcode 项目中启用“关联域”

entitlements

权利

iv. Add the proper domain entitlement, applinks:yourdomain.com, in your app

四、applinks:yourdomain.com在您的应用程序中添加适当的域权限 ,

applinks

应用链接

2. Configure your website to host the ‘apple-app-site-association' file

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

i. Buy a domain name or pick from your existing

一世。购买域名或从现有域名中选择

ii. Acquire SSL certification for the domain name (you can use CloudFlare for this!)

ii. 获取域名的 SSL 认证(您可以使用 CloudFlare!)

iii. Create structured ‘apple-app-site-association' JSON file

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

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

iv. Sign the JSON file with the SSL certification

四、使用 SSL 证书对 JSON 文件进行签名


v. Configure the file server

五、配置文件服务器

The apple-app-site-association file: - must be sent with the header ‘application/pkcs7-mime' - must be sent from the endpoint youdomain.com/apple-app-site-association - must return a 200 http code.

apple-app-site-association 文件: - 必须与标头“application/pkcs7-mime”一起发送 - 必须从端点 youdomain.com/apple-app-site-association 发送 - 必须返回 200 http 代码。

Example Express+Node:

示例 Express+Node:

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

credit: borrowed liberally from this blog post

信用:从这篇博文中大量借用

回答by mKane

Yes with iOS9 now you can deep link. Check the link for detailed explanation but I laid out the basics.

是的,现在使用 iOS9,您可以进行深度链接。检查链接以获取详细说明,但我列出了基础知识。

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

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

first you must go to your target and click capabilities. Add the associated domain.

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

Next you must upload apple-app-site-association file.

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

Basically open a JSON editor and construct something like this

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

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

Next you must support Univeral links in your app. You need to implement

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

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
                }