Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpectedly found nil while unwrapping an Optional value #19

Closed
kevvdevv opened this issue Aug 10, 2021 · 4 comments · Fixed by #22
Closed

Unexpectedly found nil while unwrapping an Optional value #19

kevvdevv opened this issue Aug 10, 2021 · 4 comments · Fixed by #22
Labels
bug Something isn't working

Comments

@kevvdevv
Copy link
Contributor

I am getting the "Unexpectedly found nil while unwrapping an Optional value" error when trying to call webAuthenticationSession in a SwiftUI project. The error points to line 185 in WebAuthenticationPresenter.swift.

func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
                return coordinator!.viewController.view.window!
            }

My code looks like this:

struct OnBoardingView: View {
    @State var showAuth = true
    var body: some View {
        checkInoReaderAuth()
            .webAuthenticationSession(isPresented: $showAuth) {
                WebAuthenticationSession(url: [my url], callbackURLScheme: nil) { url, error in
                    webAuthSessionClosure(url, error)
                }
            }
    }
}

Welcome any suggestions.

@jonthornham
Copy link

I am getting the same error. I am generating a URL and setting it in a state variable. Once the string is set I tap a button to load the BetterSafariView and I get "Unexpectedly found nil while unwrapping an Optional value" The code below shows two button. One to get the link to verify the link is there. The second to load the view.

Button(action: {
                            self.createStripeConnectAccountLink() {result in
                                if result == true {
                                    print(self.stripeAccountLink)
                                }
                            }
                                }) {
                                    Text("Get Link")
                                }
                        Button(action: {
                            self.createStripeConnectAccountLink() {result in
                                self.isWebViewPresented.toggle()
                            }
                                }) {
                                    Text("Present SafariView")
                                }
                        .safariView(isPresented: $isWebViewPresented) {
                            SafariView(
                                url: URL(string: stripeAccountLink)!,
                                configuration: SafariView.Configuration(
                                    entersReaderIfAvailable: false,
                                    barCollapsingEnabled: true
                                )
                            )
                            .preferredBarAccentColor(.pacificBlueAccent)
                            .preferredControlAccentColor(.white)
                            .dismissButtonStyle(.done)
                        }

@jonthornham
Copy link

It appears the issue occurs anytime a direct link is not added. For example this works.

Button(action: {
                            self.createStripeConnectAccountLink() {result in
                                self.isWebViewPresented.toggle()
                            }
                                }) {
                                    Text("Present SafariView")
                                }
                        .safariView(isPresented: $isWebViewPresented) {
                            SafariView(
                                url: URL(string: "http:www.github.com"!,
                                configuration: SafariView.Configuration(
                                    entersReaderIfAvailable: false,
                                    barCollapsingEnabled: true
                                )
                            )
                            .preferredBarAccentColor(.pacificBlueAccent)
                            .preferredControlAccentColor(.white)
                            .dismissButtonStyle(.done)
                        }

This does not.

Button(action: {
                            self.createStripeConnectAccountLink() {result in
                                self.isWebViewPresented.toggle()
                            }
                                }) {
                                    Text("Present SafariView")
                                }
                        .safariView(isPresented: $isWebViewPresented) {
                            SafariView(
                                url: URL(string: stripeAccountLink)!,
                                configuration: SafariView.Configuration(
                                    entersReaderIfAvailable: false,
                                    barCollapsingEnabled: true
                                )
                            )
                            .preferredBarAccentColor(.pacificBlueAccent)
                            .preferredControlAccentColor(.white)
                            .dismissButtonStyle(.done)
                        }

@kevvdevv
Copy link
Contributor Author

In digging into this a bit further, I'd propose changing

coordinator!.viewController.view.window!

to

ASPresentationAnchor()

@stleamist stleamist added the bug Something isn't working label Sep 25, 2021
@stleamist
Copy link
Owner

This issue has been fixed in v2.4.1, as a result of #22.

Thank you for your contribution and waiting :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants