How to sign in with Apple — Storyboard Version
If you have tried to upload your build since June 30, 2020, and you have a third-party sign-in (such as Facebook i.e) included in your project, you probably got into an error saying “We noticed that your app uses a third-party login service but does not offer Sign in with Apple.“ and your app is no longer eligible to be distributed in the AppStore anymore.
According to developer.apple.com, sign in with Apple provides a fast, private way to sign into apps and websites, giving people a consistent experience they can trust and the convenience of not having to remember multiple accounts and passwords.
In order to get it done, you need to follow few simple steps in order to include Apple sign in into the project.
Apple Developer Configuration
- Go to developer.apple.com, select Certificates, Identifiers & Profiles, and then select keys.
- Click on the + button (next to the keys title).
- Choose a name you would like to identify your key with, choose the Sign in with Apple option and hit continue.
Xcode Configuration
- Go to the project pane, and select your target.
- Choose Signing & Capabilities.
- Click on the + sign and select the Sign in with Apple option.
Now we are ready for some coding & design.
- Create a property of a ASAuthorizationAppleIDButton and give it your desired name (appleSignInButton i.e).
- In your storyboard, add a view object, and set the control’s class value to ASAuthorizationAppleIDButton in Xcode’s Identity Inspector.
- Connect the view to its property in the connections inspector.
After the UI is done, all we need to do is create the logic to make this magic work.
Go to your viewController and do the following steps:
- Add a target to the view we have just created
appleSignInButton.addTarget(self, action: #selector(handleAppleSignInButtonPress), for: .touchUpInside)
2. Create the handler function
@objcfunc handleAppleSignInButtonPress() { let request = ASAuthorizationAppleIDProvider().createRequest() request.requestedScopes = [.fullName, .email] let authorizationController = ASAuthorizationController(authorizationRequests: [request]) authorizationController.delegate = self authorizationController.presentationContextProvider = self authorizationController.performRequests()}
As you can see in the example above, we ask for a full name + email address, which is currently the only information we can request.
Disclaimer: The user must enable Two-Factor Authentication to use Sign in with Apple so that access to the account is secure.
Delegates methods
Now it is the time to add the delegate function which will be called after the user has finished the sign in flow and granted access.
(Note that you will get the full information only when you first sign in with a specific AppleID & AppID)
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) { if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential { let userID = appleIDCredential.user let email = appleIDCredential.email let fullName = “\(appleIDCredential.fullName?.givenName ?? “”) \(appleIDCredential.fullName?.familyName ?? “”)” }}
If the authentication fails, the authorization controller invokes the authorizationController(controller:didCompleteWithError:) delegate function to handle the error as follows:
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) { // Handle error.}
This is the result you should get:
And this is it, you made it! You now can sign in with Apple within your app.
I hope this tutorial was clear and easy to understand, if not, feel free to leave a comment below.
In the next articles, we discuss the button types and the guidelines on how we can create our custom Apple sign-in button, and some different approaches.
Make sure to check out our careers page to discover tech jobs at Takeaway.com!