欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ios中Deep Linking實(shí)例分析用法

 更新時(shí)間:2018年01月02日 08:42:59   投稿:laozhang  
本篇文章給大家分享了在IOS中Deep Linking的用法以及代碼實(shí)例,有興趣的朋友跟著學(xué)習(xí)下吧。

在 iOS 中,deep linking 實(shí)際上包括 URL Scheme、Universal Link、notification 或者 3D Touch 等 URL 跳轉(zhuǎn)方式。應(yīng)用場(chǎng)景比如常見的通知,社交分享,支付,或者在 webView 中點(diǎn)擊特定鏈接在 app 中打開并跳轉(zhuǎn)到對(duì)應(yīng)的原生頁面。

用的最多也是最常用的是通過 Custom URL Scheme 來實(shí)現(xiàn) deep linking。在 application:openURL:sourceApplication:annotation 或者 iOS9 之后引入的 application:openURL:options 中,通過對(duì) URL 進(jìn)行處理來執(zhí)行相應(yīng)的業(yè)務(wù)邏輯。一般地簡(jiǎn)單地通過字符串比較就可以了。但如果 URL 跳轉(zhuǎn)的對(duì)應(yīng)場(chǎng)景比較多,開發(fā)維護(hù)起來就不那么簡(jiǎn)單了。對(duì)此的最佳實(shí)踐是引入 router 來統(tǒng)一可能存在的所有入口。

這里介紹的一種使用 router 來組織入口的方法是來自與 kickstarter-ios 這個(gè)開源項(xiàng)目,是純 swift 開發(fā)的,而且在 talk.objc.io 上有開發(fā)者的視頻分享。

在工程,通過定于 Navigation enum,把所有支持通過 URL 跳轉(zhuǎn)的 entry point 都定義成一個(gè) case。

public enum Navigation {
 case checkout(Int, Navigation.Checkout)
 case messages(messageThreadId: Int)
 case tab(Tab)
 ...
}

在 allRoutes 字典中列出了所有的 URL 模板,以及與之對(duì)應(yīng)的解析函數(shù)。

private let allRoutes: [String: (RouteParams) -> Decode<Navigation>] = [
 "/mpss/:a/:b/:c/:d/:e/:f/:g": emailLink,
 "/checkouts/:checkout_param/payments": paymentsRoot,
 "/discover/categories/:category_id": discovery,
 "/projects/:creator_param/:project_param/comments": projectComments,
  ...
]

在 match(_ url: URL) -> Navigation 函數(shù)中通過遍歷 allRoutes,去匹配傳入的 url。具體過程是:在 match 函數(shù)內(nèi),調(diào)用 parsedParams(_ url: URL, fromTemplate: template: String) -> [String: RouteParams] 函數(shù),將分割后 template 字符串作 key,取出 url 中的對(duì)應(yīng)的 value,并組裝成 [String: RouteParams] 字典返回。最后將返回的字典 flatmap(route),即傳入對(duì)應(yīng)的解析函數(shù),最終得到 Navigation 返回

public static func match(_ url: URL) -> Navigation? {
  return allRoutes.reduce(nil) { accum, templateAndRoute in
   let (template, route) = templateAndRoute
   return accum ?? parsedParams(url: url, fromTemplate: template).flatMap(route)?.value
  }
 }
private func parsedParams(url: URL, fromTemplate template: String) -> RouteParams? {
 ...
 let templateComponents = template
  .components(separatedBy: "/")
  .filter { $0 != "" }
 let urlComponents = url
  .path
  .components(separatedBy: "/")
  .filter { $0 != "" && !$0.hasPrefix("?") }
 guard templateComponents.count == urlComponents.count else { return nil }

 var params: [String: String] = [:]
 for (templateComponent, urlComponent) in zip(templateComponents, urlComponents) {
  if templateComponent.hasPrefix(":") {
   // matched a token
   let paramName = String(templateComponent.characters.dropFirst())
   params[paramName] = urlComponent
  } else if templateComponent != urlComponent {
   return nil
  }
 }
 URLComponents(url: url, resolvingAgainstBaseURL: false)?
  .queryItems?
  .forEach { item in
   params[item.name] = item.value
 }
 var object: [String: RouteParams] = [:]
 params.forEach { key, value in
  object[key] = .string(value)
 }
 return .object(object)
}

通過 Navigation enum,把一個(gè) deep link 方式傳入的 URL,解析成一個(gè) Navigation 的 case,使得代碼具有了很高的可讀性,非常清晰明了。

以上就是小編整理的關(guān)于Deep Linking用法的詳解內(nèi)容,感謝你對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論