给iOS应用配置支持通用链接访问

2017-04-10  本文已影响136人  我是小胡胡分胡

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

上一篇下一篇

猜你喜欢

热点阅读