Advertisement
krotoff

AppDelegate for DeepLink

Jul 21st, 2017
1,463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.46 KB | None | 0 0
  1. //Requirements in AppDelegate for DeepLinking in Swift 3.
  2.  
  3. class AppDelegate: UIResponder, UIApplicationDelegate {
  4.     var window: UIWindow?
  5.     var deepLink: RemoteNotificationDeepLink?
  6.    
  7.     func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
  8.         handleDeepLink(app: app, url: url)
  9.         //...
  10.         return false
  11.     }
  12.    
  13.     func handleDeepLink(app: UIApplication, url: URL) {
  14.         guard url.host != nil else { return }
  15.         let urlString = url.absoluteString
  16.         let data = urlString.components(separatedBy: "/")
  17.        
  18.         guard data.count >= 3 else { return }
  19.         let query = data[2]
  20.         let parameter = data[3]
  21.        
  22.         guard query.range(of: "app") != nil else { return }
  23.         let userInfo = ["app": parameter ]
  24.         self.applicationHandleRemoteNotification(app, didReceiveRemoteNotification: userInfo)
  25.     }
  26.  
  27.     func applicationHandleRemoteNotification(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
  28.         if application.applicationState == UIApplicationState.background || application.applicationState == UIApplicationState.inactive {
  29.             self.deepLink = RemoteNotificationDeepLink.create(userInfo)
  30.             triggerDeepLinkIfPresent()
  31.         }
  32.     }
  33.  
  34.     func triggerDeepLinkIfPresent() -> Bool {
  35.         let ret = (self.deepLink?.trigger() != nil)
  36.         self.deepLink = nil
  37.         return ret
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement