Universal Link

通用鏈接為你提供了一些自定義 URL schemes 無法獲得的關鍵優(yōu)勢:唯一性,安全靈活,簡單私有等等

apple-app-site-association 文件

介紹:universal link 的配置文件

格式: JSON,切記不帶后綴

位置:根目錄下,或者 .well-known 目錄下,并支持 HTTPS,便于 Apple 訪問到: https://<fully qualified domain>/.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:<Application Identifier Prefix>.<Bundle Identifier>

我們先來看看一個 URI 組成:

                    hierarchical part
        ┌───────────────────┴─────────────────────┐
                    authority               path
        ┌───────────────┴───────────────┐┌───┴────┐
  abc://username:password@example.com: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 例外此規(guī)則
  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 文件順序, 優(yōu)先 /.well-known 目錄下

search.png

參考

Support Universal Links

Supporting Associated Domains

Supporting Universal Links in Your App

Xcode 文檔 Supporting Associated Domains in Your App

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容