Since long, user authentication has been growing very rapidly with advancements 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.
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)
Apple sign-in is nothing but authenticating your identity with an application that offers sign in apple id 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. Hire swift developers for your iOs devices.
Process of implementing Apple sign in apple id on an application:
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)}
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()
}
}
@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:
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.
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")
}
}
}
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!
Only the User’s Full name, Email Address and AccountID are shared on Signing in with Apple.
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.
Yes. Android apps need to take support of firebase and it fluently offers Apple sign-in.
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.
Diving deep into SwiftUI This blog post drops us into…
Corporate efficiency and customization are vital in today's fast-paced world,…
Flutter Codemagic CI/CD makes your Flutter app build, test, and…
This website uses cookies.