handling deep link scheme from openUrl not updating state correctly

41 Views Asked by At

I'm having those 2 States vars in my view.

@State private var showVerifyEmailSheet = false
@State var verifyEmailToken: String? = nil

In my view i'm handling url scheme by the following code:

ZStack {
        ....
    }
    .onOpenURL(perform: { url in
        handleIncomingURL(url)
    })


    private func handleIncomingURL(_ url: URL) {
    guard url.scheme == "myscheme" else {
        return
    }
    guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else {
        print("Invalid URL")
        return
    }
    
    guard let action = components.host, action == "verify_email" else {
        return
    }
    
    guard let token = components.queryItems?.first(where: { $0.name == "token" })?.value else {
        return
    }
    
    verifyEmailToken = token
    showVerifyEmailSheet = true
}

Once those states are changing i'm trying to show a view with the given token:

                .sheet(isPresented: $showVerifyEmailSheet, onDismiss: {
                if (LoginManager.shared.isLoggedIn()) {
                    isLoginSuccess = true
                }
            }, content: {
                VerifyEmailView(token: verifyEmailToken!)
            })

All works great when app in foreground and i'm clicking on external url. When app is dead and i'm clicking the same url app crashes because verifyEmailToken is still nil for some reason, verified it with some hardcoded instead, so it seems that the boolean state works ok because sheet is being tried to open but token is still nil althougt all checks in my handleIncomingURL func.

appreciate your help

0

There are 0 best solutions below