Mobile Application Solutions

How to Integrate Sign-in with Apple ID to Create an Account for a Third-Party App?

Sign in with Apple in iOS Devices

Introduction of Apple Sign-in Integration:

Since long, user authentication has been growing very rapidly with advancement in flexible user interface alongside consideration for growing security. Apple is known for making advancements in privacy since its start and has been peaking the market. In today’s blog we will discuss Signing in with Apple authentication module in iOS devices in any application that offers an Apple sign -functionality. 

Prerequisite:

MacOS Devices, iOS Device for testing & Apple ID Account.

Tech Stack We’re using:  iOS, Swift, XCode

Tools We’re using:  XCode (Only Available in MacOS Devices)

What is Sign-in with Apple in iOS Devices?

Apple sign-in is nothing but authenticating your identity with an application that offers Apple sign-in functionality. With this, users using an Apple account can sign-in on an application with a hassle free easy process. It is a fast and private way to sign-in into the apps which will give people the convenience and trust with the experience.

Apple sign authenticates via a Face ID or Biometrics with single button tap and also offers two factor authentication if a user has set up that in their account. Now, Apple is making a mandatory requirement of implementing Apple Sign-in on all the applications that implement google, facebook or twitter sign-in functionality.

Benefits of iOS App Development CTA

Why Sign-in with Apple in iOS Devices is Important?

    • Its privacy and security are one of the important aspects users would opt for.
    • No need to remember or store new usernames and passwords as only a single apple account can hold authentication for that application.
    • It is comparitively easy compared to traditional sign-in or registration features.
    • Mainly, it focuses on giving control to an user on what information an user wants to share to the app.
    • Users can also hide their email so with this apple will generate and share a random email to the app .

Benefits of Apple Sign-in Integration :

    • Easy to integrate compared to other authentication modules. 
    • Simple One button tap and hassle free process
    • Giving users the advantage of control over their personal data being shared.
    • Apple provides two factor authentication as part of security.

How Does Apple Sign-in Integration Work?

Process of Implementing Apple Sign in on an Application

Process of implementing Apple sign-in on an application:

1. Add Capability of Apple sign-in on your project:

Add Capability of Apple sign in on your project

2. Add sign-in with apple button programmatically:

Import AuthenticationServices

func setUpSignInAppleButton() {
  let authorizationButton = ASAuthorizationAppleIDButton()
  authorizationButton.addTarget(self, action:
#selector(handleAppleIdRequest), for: .touchUpInside)
authorizationButton.cornerRadius = 10

  //Add button on some view or stack
self.signInButtonStack.addArrangedSubview(authorizationButton)}

3. Its implementation logic function will be handleAppleIdRequest :

@objc func handleAppleIdRequest() {
if #available(iOS 13.0, *) {
let appleIDProvider = ASAuthorizationAppleIDProvider()
let request = appleIDProvider.createRequest()
request.requestedScopes = [.fullName, .email]
let authorizationController = ASAuthorizationController(authorizationRequests: [request])
authorizationController.delegate = self
authorizationController.performRequests()
}
}

4. Next step is adding Apple sign-in delegates to your LoginVC (Viewcontroller):

@available(iOS 13.0, *)
extension ViewController: ASAuthorizationControllerDelegate {

   // ASAuthorizationControllerDelegate function for authorization failed

   func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
     print(error.localizedDescription)
     self.view.makeToast(error.localizedDescription)
}

   // ASAuthorizationControllerDelegate function for successful authorization

   func authorizationController(controller: ASAuthorizationController,
didCompleteWithAuthorization authorization: ASAuthorization) {
   appDelegate.stopAnimation()
   if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {

         // Create an account as per your requirement

         let appleId = appleIDCredential.user
         let appleUserFirstName = appleIDCredential.fullName?.givenName ?? ""
         let appleUserLastName = appleIDCredential.fullName?.familyName ?? ""
         let appleUserEmail = appleIDCredential.email ?? ""

         debugPrint("apple Id", appleId)
         debugPrint("appleUser FirstName", appleUserFirstName)
         debugPrint("appleUser LastName", appleUserLastName)
         debugPrint("appleUser Email", appleUserEmail)
      }
   }
}

In this step, you will be able to retrieve Apple credentials for the user.

It will look like this on implementation:

able to retrieve Apple credentials for the users

Challenges & Solutions

Issue:

While implementation, you might notice that you will get Nil value for Email and Name after signing in. This is due to Apple restricting the app to only get the user data once on the first signing attempt. So to overcome this, you need to save the credentials you received on the first Signing attempt on User Defaults or Cloud Server you use for your application.

To save on User Default you can use this procedure:

func authorizationController(controller: ASAuthorizationController,
didCompleteWithAuthorization authorization: ASAuthorization) {
appDelegate.stopAnimation()
   if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {

      // Create an account as per your requirement

      var appleId = appleIDCredential.user

      var appleUserFirstName = appleIDCredential.fullName?.givenName ?? ""
      var appleUserLastName = appleIDCredential.fullName?.familyName ?? ""
      var appleUserEmail = appleIDCredential.email ?? ""
      if appleUserEmail == "" && appleUserFirstName == "" && appleUserLastName == "" {
      if UserDefaults.standard.value(forKey: "appleId") != nil {
         appleId = UserDefaults.standard.value(forKey: "appleId") as! String
      }
      if UserDefaults.standard.value(forKey: "appleUserFirstName") != nil {
         appleUserFirstName = (UserDefaults.standard.value(forKey: "appleUserFirstName") as? String)!
      }
      if UserDefaults.standard.value(forKey: "appleUserLastName") != nil {
         appleUserLastName = (UserDefaults.standard.value(forKey: "appleUserLastName") as? String)!
      }
      if UserDefaults.standard.value(forKey: "appleUserEmail") != nil {
      appleUserEmail = (UserDefaults.standard.value(forKey: "appleUserEmail") as? String)!
      }
   }
      else {
         UserDefaults.standard.setValue(appleId, forKey: "appleId")
         UserDefaults.standard.setValue(appleUserFirstName, forKey: "appleUserFirstName")
         UserDefaults.standard.setValue(appleUserLastName, forKey: "appleUserLastName")
         UserDefaults.standard.setValue(appleUserEmail, forKey: "appleUserEmail")
      }
   }
}

Benefits of iOS App Development CTA - 1

Conclusion

Lastly, we would like to conclude by pointing out that technology trends are growing the path in user authentication functionalities due to user’s data becoming more confidential day by day. With this, major companies like Apple are expanding their privacy to another step by bringing out Apple Sign-in and taking a new leap into securing human personal data.

Assuming all of this, we are inching near to the future where almost all the ios applications will be implementing this.

We hope this article has been informative and you have enjoyed reading it. Thank you!

FAQs:

1. What information application receives while Signing in with Apple?

Only User’s Full name, Email Address and AccountID is shared on Signing in with Apple.

2. Do I have to sign-in when I buy a new iOS Device?

No. Even on your new iOS Device you can just do a button tap on the sign-in button and it will directly ask for your password and you’re good to go.

3. Is Apple sign-in possible in Android?

Yes. Android apps needs to take support of firebase and it fluently offers Apple sign-in.

4. Do you need to put Apple sign-in on your application if you have any other third party sign-in functionality such as Google, Facebook or Twitter?

Yes. As earlier mentioned on the blog, you will need to provide the Apple sign-in functionality alongside third party sign-in functionality otherwise it might result in Apple rejecting the application for App store Release. This might be due to Apple wanting developers to provide their sign-in functionality if you are using other existing third party authentication modules.

Want to read more Blogs ?

Similar Post

Certified By

OneClick

Got more questions? Chat with our experts or schedule a call back

Bot 07:53
Hi how can i help you?