Codementor Events

# Mastering Deep Linking in iOS: Unleashing the Power of URL Schemes and Universal Links

Published Jul 25, 2023
# Mastering Deep Linking in iOS: Unleashing the Power of URL Schemes and Universal Links

Diving deep into the intricate world of mobile application development, we stumble upon a potent tool that creates a seamless user journey—Deep Linking. In the realm of iOS, this technique of connecting the dots across apps and websites takes two primary forms—URL Schemes and Universal Links. Today, we're not just scratching the surface. We're diving deep into the ocean of deep linking, uncovering advanced techniques, challenging coding examples, and sophisticated strategies that seasoned iOS engineers can leverage to exploit the full potential of deep linking.

The Magic of URL Schemes

URL Schemes—the secret incantations that help us teleport users directly into our app—are much more powerful than they appear at first glance. At the core of URL Schemes lies the concept of query parameters, powerful tools to encode complex data into URLs and drive rich inter-app communication.

For instance, suppose we're developing an e-commerce application and want to direct users straight to a particular product page. We might define a URL Scheme that looks something like this: myapp://product?id=123. To decode this URL and extract the relevant product ID, you need to add the following logic within your AppDelegate:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    if url.scheme == "myapp" {
        // Parse the query parameters
        let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
        let productId = components?.queryItems?.first(where: { $0.name == "id" })?.value

        // Use the product ID to navigate to the correct product page in your app
        navigateToProductPage(withID: productId)
        
        return true
    }
    return false
}

In more complex scenarios, we could pass entire JSON objects as query parameters. With proper URL encoding and decoding, we can deliver rich data payloads, enabling deep and meaningful inter-app communication.

Universal Links, the superheroes of the deep-linking universe, secure a smooth and integrated user experience by bridging your app and its website. They shine the brightest when we fine-tune them to perfectly synchronize with the flow and rhythm of our app's user experience.

Consider a scenario where your app requires user authentication. When a user taps a Universal Link, the app should direct authenticated users straight to the requested content, while unauthenticated users should be gently guided to a login screen.

This kind of smart navigation can be accomplished elegantly using SwiftUI 2.0's onOpenURL view modifier:

@main
struct MyApp: App {
    @StateObject var authenticationService = AuthenticationService()

    var body: some Scene {
        WindowGroup {
            if authenticationService.isLoggedIn {
                ContentView()
                    .onOpenURL { url in
                        // Handle the URL to open the correct page
                        navigateToPage(forURL: url)
                    }
            } else {
                LoginView()
            }
        }
    }
}

Depending on the user's authentication status, we render different views and handle deep linking accordingly, ensuring the user's journey aligns perfectly with your app's user experience strategy.

To master the art of deep linking, one must learn to harness the combined power of URL Schemes and Universal Links. Universal Links should serve as the primary deep linking strategy, thanks to their superior security and seamless user experience. But URL Schemes play a crucial role as a fallback mechanism for corner cases or compatibility with older iOS versions.

We can craft a function that first attempts to use a Universal Link and gracefully falls back to a URL Scheme if the Universal Link fails:

func openDeepLink() {
    let appURL = URL(string: "myapp://product?id=123")!
    let webURL = URL(string: "https://www.example.com/product/123")!

    UIApplication.shared.open(appURL, options: [:], completionHandler: { (success) in
        if !success {
            UIApplication.shared.open(webURL)
        }
    })
}

This function attempts to open the app URL using a URL Scheme. If the attempt fails, the function falls back to opening the web URL, ensuring we support deep linking in all scenarios.

Conclusion: Achieving Mastery in Deep Linking

By understanding and harnessing the full potential of URL Schemes and Universal Links, we can create intuitive user journeys that seamlessly weave through an intricate labyrinth of apps and web pages. Deep linking mastery is indeed a challenging quest, but with this guide, you are well-prepared to face the challenges and elevate your app's user experience.


Mastering deep linking requires skill, experience, and a keen understanding of app navigation and user journeys. As a seasoned independent Seenior iOS developer, I've honed these skills over countless successful projects, pushing the boundaries of what's possible with URL Schemes and Universal Links. If you're grappling with a complex deep linking challenge, need help implementing a robust deep linking strategy, or require assistance with any aspect of iOS app development, don't hesitate to contact me. Let's explore the depths of your app's potential together, and create an exceptional user experience.

Discover and read more posts from Basel Farag
get started