Handling deep/universal links

Usage guides to help you get started with deep/universal linking in the iOS Knock SDK.

Note: We Recommend taking advantage of our KnockAppDelegate to make handling deep links simpler.

Deep Links:

1. Define URL Schemes:

  • In Xcode, navigate to your app target's Info tab.
  • Add a new URL type under URL Types with a unique scheme.

Xcode Project Info

  • In your message payload that you send to Knock, include a property with a value of your deep link. The name of the property doesn't matter, so long as you know beforehand what it will be called.
  • This can also be done in your Knock Dashboard in your Payload overrides.

Deep link payload override

3. Handle Incoming URLs:

  • To handle a push notification being tapped while the app is closed:

KnockAppDelegate:

1class MyAppDelegate: KnockAppDelegate {
2     override func pushNotificationTapped(userInfo: [AnyHashable : Any]) {
3         super.pushNotificationTapped(userInfo: userInfo)
4         if let deeplink = userInfo["link"] as? String, let url = URL(string: deeplink) {
5             UIApplication.shared.open(url)
6         }
7     }
8}

Manually:

1class MyAppDelegate: UIResponder, UIApplicationDelegate {
2     open func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
3         // Check if launched from the tap of a notification
4         if let launchOptions = launchOptions, let userInfo = launchOptions[.remoteNotification] as? [String: AnyObject] {
5             // retrieve url
6             if let deeplink = userInfo["link"] as? String, let url = URL(string: deeplink) {
7                 UIApplication.shared.open(url)
8             }
9         }
10         return true
11     }
12}

  • To handle a push notification being tapped while the app is in the foreground or background:

KnockAppDelegate:

1class MyAppDelegate: KnockAppDelegate {
2     override func pushNotificationTapped(userInfo: [AnyHashable : Any]) {
3         super.pushNotificationTapped(userInfo: userInfo)
4         if let deeplink = userInfo["link"] as? String, let url = URL(string: deeplink) {
5             UIApplication.shared.open(url)
6         }
7     }
8}

Manually:

1class MyAppDelegate: UIResponder, UIApplicationDelegate {
2     open func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
3         if let deeplink = userInfo["link"] as? String, let url = URL(string: deeplink) {
4             UIApplication.shared.open(url)
5         }
6         completionHandler()
7     }
8}

Universal Links:

1. Enable Associated Domains:

  • Add the Associated Domains capability in your app target's Signing & Capabilities tab.
  • Add your domain in the format applinks:yourdomain.com.
  • Implement application(_:continue:restorationHandler:) in your AppDelegate or SceneDelegate.
1func application(_ application: UIApplication, continue userActivity: NSUserActivity,
2                  restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
3     if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
4         if let incomingURL = userActivity.webpageURL {
5             // Handle the incoming URL appropriately
6         }
7     }
8     return true
9 }

3. Server Configuration:

  • Ensure your server hosts an Apple App Site Association (AASA) file at https://yourdomain.com/.well-known/apple-app-site-association.

For more detailed instructions on configuring universal links, visit Apple's Official Documentation.