Apple Sign in always return .notFound

349 views Asked by At

I created Apple sign in like here

I Apples example all work fine.

In my project I added "Sign in with Apple" in the "Signing and Capabilities". I checked "Sign in with Apple" in the developer portal (Certificates, Identifiers & Profiles).

I get user credentials. It is part work fine.

 func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
    switch authorization.credential {
    case let appleIDCredential as ASAuthorizationAppleIDCredential:
        
        let user = User(credentials: appleIDCredential)
        self.saveUserInKeychain(user.id)
        
        let txt = "\(user.id)\n\(user.first_name) \(user.last_name)\n\(user.email)"
        let alert = UIAlertController(title: "OK", message: txt, preferredStyle: UIAlertController.Style.alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: { alertAction in
            alert.dismiss(animated: true, completion: nil)
        }))
        self.present(alert, animated: true, completion: nil)
    default:
        break
    }
}

User state here is 1.

But in the AppDelegate, when I build again I had status .notFound and error:

Error Domain=com.apple.AuthenticationServices.AuthorizationError Code=1000 "(null)"

For AppDelegate I use the same code from Apples example:

    if #available(iOS 13.0, *) {
        let appleIDProvider = ASAuthorizationAppleIDProvider()
        appleIDProvider.getCredentialState(forUserID: KeychainItem.currentUserIdentifier) { (credentialState, error) in
            print(error)
            switch credentialState {
            case .authorized:
                DispatchQueue.main.async {
                    self.window?.rootViewController?.showMainViewControllerAuth()
                }
            case .revoked, .notFound:
                DispatchQueue.main.async {
                    self.window?.rootViewController?.showMainViewControllerNoAuth()
                }
            default:
                break
            }
        }
    }

User state here is 2!

I was submitting a build to TestFlight and also had error .notFound (user state is 2). Apples example work fine in iOS simulator. But not my.

I think problem is here

    Error Domain=com.apple.AuthenticationServices.AuthorizationError Code=1000 "(null)"

Xcode 11.3. Target iOS 11.0 What could I forget?

1

There are 1 answers

0
Dim On BEST ANSWER

I solved it!

In my AppDelegate in the didFinishLaunchingWithOptions there were checks for push notifications, registrations in fabric and firebase etc. I don't know why, but you need check exists apple auth earlier then something.

This part should be the first in didFinishLaunchingWithOptions:

if #available(iOS 13.0, *) {
    let appleIDProvider = ASAuthorizationAppleIDProvider()
    appleIDProvider.getCredentialState(forUserID: KeychainItem.currentUserIdentifier) { (credentialState, error) in
        print(error)
        switch credentialState {
        case .authorized:
            DispatchQueue.main.async {
                self.window?.rootViewController?.showMainViewControllerAuth()
            }
        case .revoked, .notFound:
            DispatchQueue.main.async {
                self.window?.rootViewController?.showMainViewControllerNoAuth()
            }
        default:
            break
        }
    }
}