Learn Courses My Dashboard

Apple Login help!

Hey all! I am having an issue with apple login on my app. Stumped.

When we login using apple login the first time, it works perfectly.

But when logging in the 2nd time, the app crashes.

I think this is because apple is not providing name or email detail…

What is the best solution for this?!

Thank you so much!!!

Hi @toothdoc, can you please include the code that displays and handles your logins?

My bet is that on the first time, it sets something that works. Then, on the second time, it looks for that same value, and doesn’t handle it properly.

Thank you for the quick reply!!

Here’s the code:

@objc func applebtnclick()
{
let request = ASAuthorizationAppleIDProvider ().createRequest()
request.requestedScopes = [.email,.fullName]

       let controller  = ASAuthorizationController(authorizationRequests: [request])
       controller.delegate = self as? ASAuthorizationControllerDelegate
       controller.presentationContextProvider = self as? ASAuthorizationControllerPresentationContextProviding
       controller.performRequests()
       
   }

@available(iOS 13.0, *)
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
    switch authorization.credential {
    case let appleIDCredential as ASAuthorizationAppleIDCredential:
        let userIdentifier = appleIDCredential.user
        print(appleIDCredential.user)

        let defaults = UserDefaults.standard
        defaults.set(userIdentifier, forKey: "userIdentifier1")
        self.socialid = userIdentifier
        print (self.socialid)
        self.logintype = "A"
        self.email = appleIDCredential.email ?? ""
        print (appleIDCredential.fullName?.givenName)
        print (appleIDCredential.fullName?.familyName)
        self.firstname = (appleIDCredential.fullName?.familyName)!
        self.socialogin()
        break
    default:
        break
    }

You’re welcome, and thanks for posting some more code!

Which function handles the login, if they already signed up for an account?

Also, have you stepped through your code to see where the problem lies? Perhaps, it hits the default case, and simply breaks.

default:
        break

I suggest placing break points in the authorizationController method as well as your method that handles people that already logged in. This way you can see if your application follows your expected logic, if it goes down the paths you expect it to follow. Also, you might also want to add print statements in your other function that handles already logged in people.

Otherwise, honestly, this seems a bit above my head :sweat_smile: