給iOS應用配置支援通用連結訪問

weixin_33724059發表於2017-04-10

iOS Universal link

http://www.cocoachina.com/ios/20150902/13321.html
https://yohunl.com/ios-universal-links-tong-yong-lian-jie/

application:continueUserActivity:restorationHanler:
application:willContinueUserActivityWithType:

1, 新增域名

Capabilities
applinks:www.domain.com
applinks:domain.com

開啟Capabilities開關後,自動生成xxx.entitlements檔案

2,設定好bundle identifier

com.domain.demo2

3,建立apple-app-site-association檔案

{
"applinks":{
"apps":[],
"details":[
{
    "appID":"9JVMQ7S4HD.com.domain.demo2"
    "paths":["*"]
},
{
    "appID":"9JVMQ7S4HD.com.domain.demo3"
    "paths":["*"]
}
]
}
}


{
  "applinks": {
    "apps": [],
    "details": [
      {
        "appID": "KS7QAPBMXA.com.jianshu.Hugo",
        "paths": [ "/p/*", "/c/*", "/u/*", "/nb/*", "/collection/*", "/users/*", "/notebooks/*", "NOT /users/auth/*" ]
      }
    ]
  }
}

上傳apple-app-site-association檔案到web server伺服器根目錄下面。
需要https支援。客戶端訪問需要通過https訪問才可以。並且不能重定向。

4,測試。

在appdelegate檔案中實現協議application:continueUserActivity:restorationHanler:方法

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray * __nullable restorableObjects))restorationHandler
{
    if([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
        NSURL *webURL = userActivity.webpageURL;
        if ([webURL.host isEqualToString:@"domain.com"]) {
        //跳轉到對應的頁面
        } else {
            //不能識別,用瀏覽器開啟
            [[UIApplication sharedApplication] openURL:webURL];
        }
    }
    return YES;
}

5,例子

http://www.jackivers.me/blog/2015/9/17/list-of-universal-link-ios-9-apps
->在聊天記錄中點選連結:https://twitter.com/mchappell51在微信瀏覽器中開啟
->然後點選導航欄右上角按鈕,在safri瀏覽器中開啟
->如果安裝了twitter,會自動跳轉到twitter app,如果沒有安裝,則會跳轉到該網頁。

自定義連結

iOS URL types
URL identifier
URL schemes
http://www.jianshu.com/p/feecacacdefd

相關文章