iOS Universal Links
功能简介
举个栗子:用户在手机上通过Safari
浏览器�查看简书
的某篇文章时,会打开用户手机上的简书App
并进入到这篇文章,从而实现了从浏览器到App的无缝链接
前提条件
要使用iOS Universal Links
功能需要如下前提条件:
-
iOS
9+ - 要有一个网站,并且支持
Https
配置
JSON配置文件
命名为apple-app-site-association
的JSON配置文件,放到网站的根目录,可以通过Https访问,以简书
为例子http://jianshu.com/apple-app-site-association
{
"applinks": {
"apps": [],
"details": [
{
"appID": "KS7QAPBMXA.com.jianshu.Hugo",
"paths": [ "/p/*", "/collection/*", "/users/*", "/notebooks/*" ]
}
]
}
}
简单解释一下:如果在浏览器中访问的某个简书页面的url匹配paths
规则,则尝试打开Team ID
为KS7QAPBMXA
且Bundle ID
为com.jianshu.Hugo
的App,并且将url传递给App。
iOS App 配置

iOS接收并处理url参数
AppDelegate
import UIKit
extension AppDelegate {
func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
let webpageURL = userActivity.webpageURL! // Always exists
if !handleUniversalLink(URL: webpageURL) {
UIApplication.sharedApplication().openURL(webpageURL)
}
}
return true
}
private func handleUniversalLink(URL url: NSURL) -> Bool {
if let components = NSURLComponents(URL: url, resolvingAgainstBaseURL: true), let host = components.host, let pathComponents = components.path?.pathComponents {
switch host {
case "domain.com":
if pathComponents.count >= 4 {
switch (pathComponents[0], pathComponents[1], pathComponents[2], pathComponents[3]) {
case ("/", "path", "to", let something):
if validateSomething(something) {
presentSomethingViewController(something)
return true
}
default:
return false
}
}
default:
return false
}
}
return false
}
}
返回true
表示处理成功,会打开App并进入到对应的页面;返回false
表示处理失败,会停留在Safari页面。
网友评论