Universal Link

通用链接为你提供了一些自定义 URL schemes 无法获得的关键优势:唯一性安全灵活简单私有等等

apple-app-site-association 文件

介绍:universal link 的配置文件

格式: JSON,切记不带后缀

位置:根目录下,或者 .well-known 目录下,并支持 HTTPS,便于 Apple 访问到: https:///.well-known/apple-app-site-association

例子:

{
  "applinks": {
      "details": [
           {
             "appIDs": [ "ABCDE12345.com.example.app", "ABCDE12345.com.example.app2" ],
             "components": [
               {
                  "#": "no_universal_links",
                  "exclude": true,
                  "comment": "Matches any URL whose fragment equals no_universal_links and instructs the system not to open it as a universal link"
               },
               {
                  "/": "/buy/*",
                  "comment": "Matches any URL whose path starts with /buy/"
               },
               {
                  "/": "/help/website/*",
                  "exclude": true,
                  "comment": "Matches any URL whose path starts with /help/website/ and instructs the system not to open it as a universal link"
               },
               {
                  "/": "/help/*",
                  "?": { "articleNumber": "????" },
                  "comment": "Matches any URL whose path starts with /help/ and which has a query item with name 'articleNumber' and a value of exactly 4 characters"
               }
             ]
           }
       ]
   }
}

appIDs:.

我们先来看看一个 URI 组成:

                    hierarchical part
        ┌───────────────────┴─────────────────────┐
                    authority               path
        ┌───────────────┴───────────────┐┌───┴────┐
  abc://username:[email protected]:123/path/data?key=value&key2=value2#fragid1
  └┬┘   └───────┬───────┘ └────┬────┘ └┬┘           └─────────┬─────────┘ └──┬──┘
scheme  user information     host     port                  query         fragment

components: 见举例中 commont

  1. / 匹配 URL path
  2. # 匹配 URL fragment
  3. ? 匹配 URL query items
  4. exclude 例外此规则
  5. comment 注释
  6. * 匹配域名下所有 path
  7. /aa/bb/ 特定的链接
  8. /aa/bb/* 特定链接下的子集

Handle Universal Links

Xcode 配置:打开项目--> 选择当前 Target --> Signing & Caoabilities --> + Associated Domains --> 以 applinks 开头+apple-app-site-association文件存储的域名,例如 applinks:www.apple.com

代码捕获:

func application(_ application: NSApplication,
                     continue userActivity: NSUserActivity,
                     restorationHandler: @escaping ([NSUserActivityRestoring]) -> Void) -> Bool
{
  // handle action
  if userActivity.activityType == NSUserActivityTypeBrowsingWeb {  // handle universal links
      guard let incomingURL = userActivity.webpageURL,
        let components = NSURLComponents(url: incomingURL, resolvingAgainstBaseURL: true) else { return false }
      guard let path = components.path else {
          return false
      }
      print("path = \(path), comp = \(components)")
    }
    return true
}

注意事项

  1. 缓存问题:如果修改了 apple-app-site-association ,重新运行没有生效,则建议卸载 App 重新安装。

  2. apple-app-site-association 文件摈弃了旧的 path 字段改为 components,更灵活好用

  3. Apple 查找顺序 apple-app-site-association 文件顺序, 优先 /.well-known 目录下

search.png

参考

Support Universal Links

Supporting Associated Domains

Supporting Universal Links in Your App

Xcode 文档 Supporting Associated Domains in Your App

你可能感兴趣的:(Universal Link)