0
0
iOS Swiftmobile~20 mins

Deep linking in iOS Swift - Mini App: Build & Ship

Choose your learning style9 modes available
Build: DeepLinkDemo
This screen shows how to handle deep links in an iOS app. When the app opens via a URL, it displays the path and query parameters on screen.
Target UI
-------------------------
| Deep Link Demo        |
|-----------------------|
| URL Path:             |
|                       |
| Query Params:         |
|                       |
|                       |
|                       |
|                       |
-------------------------
Handle incoming URLs using SceneDelegate method
Parse URL path and query parameters
Display the path and parameters on the screen
Show a default message if app opened normally without URL
Starter Code
iOS Swift
import SwiftUI

@main
struct DeepLinkDemoApp: App {
    @StateObject var deepLinkModel = DeepLinkModel()

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(deepLinkModel)
        }
    }
}

class DeepLinkModel: ObservableObject {
    @Published var path: String = "No URL opened"
    @Published var queryParams: String = ""
}

struct ContentView: View {
    @EnvironmentObject var deepLinkModel: DeepLinkModel

    var body: some View {
        VStack(alignment: .leading, spacing: 20) {
            Text("URL Path:")
                .font(.headline)
            Text(deepLinkModel.path)
                .foregroundColor(.blue)
            Text("Query Params:")
                .font(.headline)
            Text(deepLinkModel.queryParams)
                .foregroundColor(.green)
            Spacer()
        }
        .padding()
    }
}

// TODO: Add URL handling in SceneDelegate or equivalent
Task 1
Task 2
Task 3
Solution
iOS Swift
import SwiftUI

@main
struct DeepLinkDemoApp: App {
    @StateObject var deepLinkModel = DeepLinkModel()

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(deepLinkModel)
        }
        .onOpenURL { url in
            // Parse URL path
            deepLinkModel.path = url.path.isEmpty ? "(empty path)" : url.path

            // Parse query parameters
            if let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
               let queryItems = components.queryItems, !queryItems.isEmpty {
                let params = queryItems.map { "\($0.name)=\($0.value ?? "")" }.joined(separator: ", ")
                deepLinkModel.queryParams = params
            } else {
                deepLinkModel.queryParams = "No query parameters"
            }
        }
    }
}

class DeepLinkModel: ObservableObject {
    @Published var path: String = "No URL opened"
    @Published var queryParams: String = ""
}

struct ContentView: View {
    @EnvironmentObject var deepLinkModel: DeepLinkModel

    var body: some View {
        VStack(alignment: .leading, spacing: 20) {
            Text("URL Path:")
                .font(.headline)
            Text(deepLinkModel.path)
                .foregroundColor(.blue)
            Text("Query Params:")
                .font(.headline)
            Text(deepLinkModel.queryParams)
                .foregroundColor(.green)
            Spacer()
        }
        .padding()
    }
}

This solution uses SwiftUI's .onOpenURL modifier to handle deep links when the app opens or is already running. The URL's path and query parameters are extracted and stored in a shared DeepLinkModel observable object. The ContentView observes this model and updates the UI to show the current URL path and parameters.

This approach is simple and fits well with SwiftUI's reactive design. It avoids the older SceneDelegate method and uses modern SwiftUI lifecycle APIs.

Final Result
Completed Screen
-------------------------
| Deep Link Demo        |
|-----------------------|
| URL Path:             |
| /product/details      |
| Query Params:         |
| id=123, ref=abc       |
|                       |
|                       |
|                       |
-------------------------
When the app opens via a URL like myapp://product/details?id=123&ref=abc, the screen updates to show the path '/product/details' and query parameters 'id=123, ref=abc'.
If the app opens normally without a URL, it shows 'No URL opened' and no query parameters.
Stretch Goal
Add support to handle deep links when the app is already running in the background.
💡 Hint
Use the .onOpenURL modifier in your SwiftUI App struct to handle URLs anytime the app receives them.