Search code examples
iosswiftswiftuiapple-sign-in

SwiftUI SignInWithAppleButton onCompletion never get called on M1 Mac


The alternative title is "SwiftUI Sign in with Apple button does not work on M1 Mac".

Environment macOS v11.4, Xcode Version 12.5.1, MacBook Pro (13-inch, M1, 2020)

The following SignInWithAppleButton code block works fine on iOS simulators and real devices. However, the completion handler never gets triggered when running it on an M1 Mac. Reading the documentation and it seems to be available on macOS 11.0+.

enter image description here

Is this a known SwiftUI issue because there is no such issue when I am using an UIKit implementation?

SignInWithAppleButton(
    .continue,
    onRequest: { request in
        request.requestedScopes = [.email]
        print(">>> auth on request") // <- Get called on click
    },
    onCompletion: { result in
        print(">>> auth completed") // <- Never get called
        switch result {
        case let .success(auth):
            switch auth.credential {
            case let credential as ASAuthorizationAppleIDCredential:
                let identityToken = credential.identityToken
                if let idToken = identityToken {
                    Dummy.signUpOrLogInWithApple(idToken: idToken)
                }
            default:
                print(">>> default case")
            }
        case let .failure(error):
            print(">>> Sign in with Apple failed", error.localizedDescription)
        }
    }
)

enter image description here

Not a solution for this problem but a workaround is to continue using the UIKit version of “Sign in with Apple”,~~ https://github.com/Q42/iOS-Demo-SignInWithApple-SwiftUI/blob/master/iOS-Demo-SignInWithApple-SwiftUI/SignInWithAppleButton.swift

I think it is most likely a SwiftUI issue. Tried a wrapped UIKit SignInWithAppleButton however the problem persists. I wonder if this is caused by the missing of presentationAnchor which makes it unusable on macOS.


Solution

  • IF THIS HAPPENED TO YOU JUST RESTART YOUR MAC!!! and then it would work just as expected. No need to use a wrapped UIKit SignInWithAppleButton. I think it is most likely an iOS on M1 compatibility issue.

    enter image description here

    Tried a wrapped UIKit SignInWithAppleButton however the problem persists. I wonder if this is caused by the missing of presentationAnchor which makes it unusable on macOS.

    Not a solution for this problem but a workaround is to continue using the UIKit version of “Sign in with Apple”,~~ https://github.com/Q42/iOS-Demo-SignInWithApple-SwiftUI/blob/master/iOS-Demo-SignInWithApple-SwiftUI/SignInWithAppleButton.swift