Skip to content

Latest commit

 

History

History
77 lines (63 loc) · 2.61 KB

Deeplinks.md

File metadata and controls

77 lines (63 loc) · 2.61 KB

Deeplinks

Depending on the authentication flows you offer in your app (for instance: Email Magic Links or Password Reset By Email), you may need to configure Stytch and your application to handle deeplinks.

Step-By-Step

Adding a new URL Scheme to your app.

In Xcode navigate to your target's "Info" tab and add a new entry under "URL Types" xcode_target_info

Click the plus sign and choose a value to add to the "URL Schemes" field. new_url_type

Adding your links to Stytch

Adding your links as valid Redirect URLs in the Stytch Dashboard deeplink-urls-stytch

Further Reading on Deeplinks

For a more advanced implementation using Universal Links please refer to the following reading.

IOS Deep linking: URL Scheme vs Universal Links

Supporting Associated Domains

Anatomy of a Stytch Redirect URL

Anatomy of a Stytch Redirect URL

Handling the Deeplink

In UIKit

In your SceneDelegate file add the following code

func scene(_: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let url = URLContexts.first?.url else {
        return
    }

    handle(url: url)
}

In SwiftUI

Add the following code to your root view.

.onOpenURL { url in
    handle(url: url)
}

Handle the Deeplink

This handle method can be used for either UIKit or SwiftUI.

import StytchCore

func handle(url: URL) {
    Task {
        do {
            switch try await StytchClient.handle(url: url, sessionDuration: 5) {
            case let .handled(response):
                switch responseData {
                case let .auth(response):
                    print("handled .auth: \(response.session) - \(response.user)")
                case let .oauth(response):
                    print("handled .oauth: \(response.session) - \(response.user)")
                }
            case .notHandled:
                print("not handled")
            case let .manualHandlingRequired(tokenType, token):
                print("manualHandlingRequired: tokenType: \(tokenType) - token: \(token)")
            }
        } catch {
            print("handle url error: \(error)")
        }
    }
}

Further Reading

For more information on the structure of Redirect URLs, consult our guide.