iOS Deep Linking

Deep Linking Nativo e Gestione URL Custom con App Links e Universal Links in SwiftUI

Il **Deep Linking** permette di aprire direttamente schermate specifiche di un'app iOS cliccando su un link da un browser, una chat o un'email. Su iOS, lo standard moderno ed ultra-sicuro è rappresentato dagli **Universal Links**, integrati nativamente in SwiftUI attraverso il modifier .onOpenURL.

1. Il File `apple-app-site-association` (AASA)

Per configurare Universal Links sul tuo dominio web (es. diallodev.online), devi ospitare nella cartella .well-known/apple-app-site-association il seguente file JSON senza estensione:

{
  "applinks": {
    "apps": [],
    "details": [
      {
        "appID": "TEAMID12345.online.diallodev.app",
        "paths": [ "/post/*", "/user/*", "/AI" ]
      }
    ]
  }
}

2. Gestione degli URL in SwiftUI con `.onOpenURL`

In SwiftUI l'instradamento della rotta avviene intercettando la chiusura della lambda onOpenURL sul root container dell'app:

import SwiftUI

enum AppRoute: Hashable {
    case profile(userId: String)
    case post(postId: String)
    case aiWorkspace
}

@main
struct DeepLinkApp: App {
    @State private var navigationPath = NavigationPath()

    var body: some Scene {
        WindowGroup {
            NavigationStack(path: $navigationPath) {
                HomeView()
                    .navigationDestination(for: AppRoute.self) { route in
                        switch route {
                        case .profile(let userId):
                            UserProfileView(userId: userId)
                        case .post(let postId):
                            PostDetailView(postId: postId)
                        case .aiWorkspace:
                            AIWorkspaceView()
                        }
                    }
            }
            .onOpenURL { url in
                handleIncomingURL(url)
            }
        }
    }

    private func handleIncomingURL(_ url: URL) {
        guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else { return }
        let path = components.path

        if path.hasPrefix("/user/") {
            let userId = url.lastPathComponent
            navigationPath.append(AppRoute.profile(userId: userId))
        } else if path.hasPrefix("/post/") {
            let postId = url.lastPathComponent
            navigationPath.append(AppRoute.post(postId: postId))
        } else if path == "/AI" {
            navigationPath.append(AppRoute.aiWorkspace)
        }
    }
}

3. URL Scheme Custom come Fallback

Negli scenari in cui gli Universal Links non siano disponibili, è possibile registrare un Custom URL Scheme (es. diallodev://) negli *URL Types* di Xcode per gestire l'apertura tramite collegamenti rapidi.

Conclusione

L'integrazione fluida dei deep link garantisce un passaggio trasparente dal web al mondo mobile. Per implementare la navigazione tramite Universal Links nelle tue app iOS, scrivi a diallooyunus@gmail.com.

Yunus Diallo (DialloDev)

Fondatore di Dywtal Digital a Parma (Italia), sviluppatore iOS con esperienza in architetture SwiftUI e navigazione complessa.