Scheme Deep Link
點擊URL轉跳APP的某頁面或是啟動某項功能
比如Line所釋出的 line://msg/text/+文字
可以傳送訊息直接帶文字過去Line APP啟動
以下簡易示範
stevenApp://test.com:80/12345;param?account=1&password=2#fragment
監聽Scheme點擊事件
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
let result = ApplicationDelegate.shared.application(app, open: url, options: options)
if url.host == nil {
return true;
}
print("url:\(url)")
print("scheme:\(url.scheme ?? "")")
print("host:\(url.host ?? "")")
print("port:\(url.port ?? 0)")
print("pathComponents:\(url.pathComponents)")
print("query:\(url.query ?? "")")
print("fragment:\(url.fragment ?? "")")
return result
}
- scheme : 你APP設定得連結,比如Line就是line://
- host : 主URL
- port : port號 有的沒有就是nil
- pathComponents : 子分層地址
- query : GET 傳送參數
- fragment : 分層
url:stevenApp://test.com:80/12345;param?account=1&password=2#fragment
scheme:stevenApp
host:test.com
port:80
pathComponents:["/", "12345;param"]
query:account=1&password=2
fragment:fragment
url query解析器
擴展一個URL+extension
自動把Get傳得參數轉為Dictionary
extension URL {
/// 解析URL GET轉Dictionary
var params: [String: String]? {
if let urlComponents = URLComponents(url: self, resolvingAgainstBaseURL: true) {
if let queryItems = urlComponents.queryItems {
var params = [String: String]()
queryItems.forEach{
params[0.name] =0.value
}
return params
}
}
return nil
}
}
AppDelegate
print("url params:\(String(describing: url.params))")
demo
url params:Optional(["password": "2", "account": "1"])