At WWDC 2019( WWDC is Apple’s annual Worldwide Developers Conference ), Apple provides a convenient, quick way to sign in to apps and websites, i.e. With Apple sign up. Apple Sign-In allows you to set up a user account with a name, checked email address, and unique stable identifiers in your application that allows a user to sign in with their Apple ID to your app.
-It debuts with ios13
Ques- Is sign-in with apple mandatory?
Ans- For all applications that provide third-party login options to users( Like- Google Sign-in, Log in with Amazon, Facebook Login, Sign-in with Twitter, Instagram ) than Apple’s Latest “Sign in with Apple” login feature will be mandatory.
Benefits of Sign-in with Apple:
1. Privacy
2. Security
3. Support Multiple Platforms( It Also works on iOS, macOS, tvOS, and watchOS )
4. Non-Repeatable Logins
Step 1- Open your existing Xcode Project ( if you don’t have then create a new Xcode Project.)
Step 2- Configure sign-in with Apple:
- Now, go to Target and select ‘capability’, then add sign-in with apple Capability in Your project.
- After that enable this Sign in with Apple functionality in the App identifier in Appstore connect( Certificates, Identifiers & Profiles Identifiers => Identifiers => You app identifier )
- After that add Keys for Apple Sign in (in Certificates, Identifiers & Profiles Identifiers => Keys=> Create Key ).
- Now, Enter the keys name, and don’t forget to check the Apple Sign In option. After that, press the configure button in the same row and pick the ID of our app
- Now, Save the Process and Register the Key after selecting your bundle id.
Step 3- Now, add sign-in with apple Button in your required view Controller.
- Firstly import the Authentication Services framework( it is apple framework that handles the authentication, it also includes UI elements)
- We can also create our custom sign-in with apple button, but here we create a button using
ASAuthorizationAppleIDButton() button
- We have provided a target function for the button that will be called when the button is Tapped. Here target function name is “appleBtnPressed()”.
- We create a request first and ask the user in requestScopes for a .fullName and .email. This is essentially what we would like to obtain from the Apple ID profile of the user. This is totally optional, and if you really need to, just grab this information.
- Then we pass our request to get the result of the verification, we can initialise the ASAuthorizationController with ASAuthorizationControllerDelegate protocol, we access the delegate functions.
Now, add below code according to previous steps:
import UIKit import AuthenticationServices class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() createAppleSigninBtn() } func createAppleSigninBtn() { let appleBtn = ASAuthorizationAppleIDButton() appleBtn.frame = CGRect(x: 0, y: 0, width: 250, height: 50) appleBtn.center = view.center view.addSubview(appleBtn) appleBtn.addTarget(self, action: #selector(appleBtnPressed), for: .touchUpInside) } @objc private func appleBtnPressed() { let appleIDProvider = ASAuthorizationAppleIDProvider() let dataRequest = appleIDProvider.createRequest() dataRequest.requestedScopes = [.fullName, .email] let authorizationController = ASAuthorizationController(authorizationRequests: [dataRequest]) authorizationController.delegate = self authorizationController.presentationContextProvider = self authorizationController.performRequests() }
- After the user has successfully signed in, we get the user credentials within the authorizationController did CompleteWithAuthorization. Check if the credential is ASAuthorizationAppleIDCredential, and save the returned user credentials for the estate.
- For only now, we save user information in user default but it is not secure maybe you will use keychain or maybe your own secure database.
- Now handle the didCompleteWithError method according to your requirement. ( like- show an alert )
- For the present the Authorization controller we use presentationAnchor method and we have to provide the anchor window:
extension ViewController: ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding { func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) { if let appleID_Credential = authorization.credential as? ASAuthorizationAppleIDCredential { let userIdentifier = appleID_Credential.user let fullName = appleID_Credential.fullName let email = appleID_Credential.email print("User id is \(userIdentifier) \n Full Name is \(String(describing: fullName)) \n Email id is \(String(describing: email))") let Savedefaults = UserDefaults.standard Savedefaults.set(userIdentifier, forKey: "uIdentifier") //Save the UserIdentifier in keychain or your secure server because it is not secure // Navigate to other VC according to your Project needs } } func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor { return self.view.window! } }
- Until now, the user has created a new account, we access the user’s email, name, and other requested data.
- In the last step, we can use the userIdentifier that we obtained from the ASAuthorizationAppleIDCredential object to check the user credential state.
- For a userIdentifier, we can get credential state by calling getCredentialState(forUserID: completion:) method:
let appleSignIDProvider = ASAuthorizationAppleIDProvider() appleSignIDProvider.getCredentialState(forUserID: userIdentifier) { (credentialState, error) in switch credentialState { case .authorized: // The Apple ID credential is valid. Show HomeView ui’s break case .revoked: // The Apple ID credential is revoked. Show SignInView ui’s. break case .notFound: // No credential was found. Show SignInView ui’s break default: break } }