美文网首页
[iOS] Universal Links 配置

[iOS] Universal Links 配置

作者: 巨馍蘸酱 | 来源:发表于2022-07-25 15:31 被阅读0次

Universal Links 配置

在 iOS9 之前,使用 URL Scheme 从浏览器启动 App
在 iOS9 推出了 通用链接 (Universal Links), 通过 链接直接启动 APP

URL Scheme

通过 Safari 打开自己

Targets -> 项目 -> info -> URL Types 中, 新增一个 URL Schemes (如: haha), 通过 Safari 访问 haha://可选参数 可以打开该 APP

其他 APP 打开 haha

Info.plist 配置 LSApplicationQueriesSchemesArray 把允许的 URL Scheme 添加进去

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>haha</string>
</array>

由于 iOS 9 系统策略更新,限制了 http 协议的访问, 将要使用的 URL Schemes 列为白名单

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>haha</string>
</array>

打开其他 APP

let urlString = "haha://可选参数"
let url = URL(string: urlString)
if UIApplication.shared.canOpenURL(url!) {
    UIApplication.shared.open(url!)
}

Universal Links

先看下微信的 Universal Links

{
    "applinks": {
        "apps": [],
        "details": [
            {
                "appID": "532LCLCWL8.com.tencent.xin",
                "paths": ["/cgi-bin/newreadtemplate", "/app/*"]
            }, {
                "appID": "8P7343TG54.com.tencent.wc.xin",
                "paths": ["/cgi-bin/newreadtemplate", "/*"]
            }, {
                ...
            }, {
                "appID": "HKB8WPFBR9.com.tencent.xin.sdksample.db",
                "paths": ["/sdksample/*"]
            }
        ]
    }
}

配置

  1. 创建 apple-app-site-association 文件 (不要.json后缀名), application/json MIME 类型的纯文本文件
{
    "applinks": {
        "apps": [],
        "details": [
            {
                "appID": "9JA89QQLNQ.com.apple.wwdc",
                "paths": [ "/wwdc/news/", "NOT /videos/wwdc/2015/*"]
            }, {
                "appID": "ABCD1234.com.apple.wwdc",
                "paths": [ "*" ]
            }, {
                "appID": "你的TeamId.你的BundleId",
                "paths": [ "NOT /app/不能访问/*", "/wx_app/微信访问/*", "qq_app/qq访问/*" ]
            }
        ]
    }
}
  1. 将文件放在服务器的根目录或 .well-known 子目录中
  2. 通过 HTTPS 访问 https://<domain>/apple-app-site-associationhttps://<domain>/.well-known/apple-app-site-association 可下载文件或直接显示内容
  3. 登录 Apple Developer 配置 Capabilities 启用 Associated Domains
  4. Xcode 中 Targets -> 项目 -> Capabilities 添加 Associated Domainsapplinks:域名 (如: applinks:www.mywebsite.com / applinks:*.mywebsite.com)
  5. 测试 https://+applinks配置的域名+apple-app-site-association里的paths (如: https://domain.com/wx_app/123haha)
    • 手机备忘录填写该链接并点击, 成功跳转 APP
    • Safari 打开 链接, 上方提示 打开 APP

备注

  • nginx 配置
location /apple-app-site-association {
    charset UTF-8;
    default_type text/html;
    return 200 '{\"applinks\":{\"apps\":[],\"details\":[{\"appID\":\"teamId.bundleId\",\"paths\":[\"/*\"]}]}}'
}
  • 当用户点击通用链接时,iOS 会启动您的应用并向其发送一个 NSUserActivity 对象

鸣谢

相关文章

网友评论

      本文标题:[iOS] Universal Links 配置

      本文链接:https://www.haomeiwen.com/subject/mlmoirtx.html