Apple Sign In with Swift
Posted By : Lucky Mehndiratta | 21-Dec-2020
Apple Sign In with Swift
Almost every mobile application & web needs to authenticate or verify the user before landing on the main features of the mobile application and this will be done in many ways like phone number, email address & other third party authenticate to ensure the end-user authenticity. but this way a little bit more complete & to tricky way for the end-user. So, Apple gives us the best way to authenticate the user with minimum cost on the mobile application as well as on the web.
Let's Start
Enable Sign in
Before proceeding further first we need to enable Sign in either through Apple Developer Account or using Xcode. After that we need to add an entitlement file to our project if we enable using Xcode then it manages automatically but if we enable using Apple Developer Account then we need to add this in our project. (Note:- you must have an apple developer account with Apple enrollment for both ways)
Also Read: Choosing The Right Data Structure In Swift
Setup & Coding
First, we need to import the Authentication Servcies provided by Apple in the respective viewcontroller/screen where we want to give a login option to the user.
import Authentication Services
add a view in your UI Design on which we need to set up Sign in with Apple button
Function to setup Apple Sign In button
func setUpSignIn() {
let authorizationButton = ASAuthorizationAppleIDButton()
authorizationButton.addTarget(self, action: #selector(appleRequest), for: .touchUpInside)
authorizationButton.cornerRadius = 10 // based on your requirement
self.yourview.addSubview(authorizationButton)
}
Function to create a request
@objc func appleRequest() {
let provider = ASAuthorizationAppleIDProvider()
let createRequest = provider.createRequest()
createRequest.requestedScopes = [.fullName, .email]
let controller = ASAuthorizationController(authorizationRequests: [createRequest])
controller.delegate = self.controller.performRequests()
}
Add a delegate to your controller ASAuthorizationControllerDelegate
function called after successful sign in
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
if let credential = authorization.credential as? ASAuthorizationAppleIDCredential{
let userId = credential.user
let fullName = credential.fullName
let email = credential.email
print(userId, fullName, email) // use these based on your requirement & do other task
}
}
function called when the error occurs
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
// Handle error based on your requirement
}
Awesome!!! We complete Apple Sign In
Official Links:-
1 https://developer.apple.com
2 https://developer.apple.com/documentation/authenticationservices/implementing_user_authentication_with_sign_in_with_apple
Thank you for reading!
We are a full-scale Mobile App development company that specializes in building scalable web and mobile applications for multiple platforms. Our development team uses advanced frameworks, tools, SDKs and agile methodologies to develop feature-rich business applications with custom features. Our end-to-end mobile application development services render support for both Android and iOS platforms. We have successfully completed several full-fledged Android and iOS application development projects for startups, SMEs, and large-scale enterprises. For project-related queries, drop us a line at [email protected].
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Lucky Mehndiratta
He worked on swift language, UI Design, Api. He is quick in his work and performs at his best.