0
0
iOS Swiftmobile~5 mins

Deep linking in iOS Swift

Choose your learning style9 modes available
Introduction

Deep linking lets your app open specific screens directly from a link. It helps users jump straight to what they want inside your app.

You want users to open a product page in your shopping app from a web link.
You want to send a notification that opens a specific chat in your messaging app.
You want to share a link that opens a particular article inside your news app.
You want to handle links from emails that open a special offer screen in your app.
Syntax
iOS Swift
func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    // Handle the URL and navigate
    return true
}
This method is called when your app is opened via a URL scheme.
You parse the URL to decide which screen to show.
Examples
This example checks if the URL scheme is "myapp" and the host is "product". Then it extracts the product ID from the URL path.
iOS Swift
func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    if url.scheme == "myapp" {
        if url.host == "product" {
            let productId = url.lastPathComponent
            // Navigate to product screen with productId
        }
    }
    return true
}
This example shows how to handle universal links using NSUserActivity for web URLs.
iOS Swift
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
        if let url = userActivity.webpageURL {
            // Handle universal link URL
        }
    }
    return true
}
Sample App

This simple app delegate handles a deep link with scheme "myapp" and host "profile". It prints the user ID from the URL path.

iOS Swift
import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        return true
    }

    func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        if url.scheme == "myapp" && url.host == "profile" {
            let userId = url.lastPathComponent
            print("Open profile screen for user: \(userId)")
            // Here you would navigate to the profile screen in your app
        }
        return true
    }
}
OutputSuccess
Important Notes

Always register your URL schemes in your app's Info.plist file.

Test deep links by typing them in Safari or using the command line.

Universal links require additional setup with your website and Apple App Site Association file.

Summary

Deep linking lets users open specific parts of your app from links.

Use the application(_:open:options:) method to handle custom URL schemes.

Universal links use NSUserActivity and require website setup.