Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
extension LoginViewController: ASAuthorizationControllerDelegate {
// Authorization Failed
@available(iOS 13.0, *)
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
}
// Authorization Succeeded
@available(iOS 13.0, *)
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
}
}
func appleLoginButton() {
if #available(iOS 13.0, *) {
let appleLoginBtn = ASAuthorizationAppleIDButton(type: .signIn, style: .whiteOutline)
appleLoginBtn.addTarget(self, action: #selector(actionHandleAppleSignin), for: .touchUpInside)
self.view.addSubview(appleLoginBtn)
// Setup Layout Constraints to be in the center of the screen
appleLoginBtn.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
appleLoginBtn.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
appleLoginBtn.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
appleLoginBtn.widthAnchor.constraint(equalToConstant: 200),
appleLoginBtn.heightAnchor.constraint(equalToConstant: 40)
])
}
}
let appleIDProvider = ASAuthorizationAppleIDProvider()
appleIDProvider.getCredentialState(forUserID: userIdentifier) { (credentialState, error) in
switch credentialState {
case .authorized:
// Credential is valid
// Continiue to show 'User's Profile' Screen
break
case .revoked:
// Credential is revoked.
// Show 'Sign In' Screen
break
case .notFound:
// Credential not found.
// Show 'Sign In' Screen
break
default:
break
}
}
func appleCustomLoginButton() {
if #available(iOS 13.0, *) {
let customAppleLoginBtn = UIButton()
customAppleLoginBtn.layer.cornerRadius = 20.0
customAppleLoginBtn.layer.borderWidth = 2.0
customAppleLoginBtn.backgroundColor = UIColor.white
customAppleLoginBtn.layer.borderColor = UIColor.black.cgColor
customAppleLoginBtn.setTitle("Sign in with Apple", for: .normal)
customAppleLoginBtn.setTitleColor(UIColor.black, for: .normal)
customAppleLoginBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .semibold)
customAppleLoginBtn.setImage(UIImage(named: "apple"), for: .normal)
customAppleLoginBtn.imageEdgeInsets = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 12)
customAppleLoginBtn.addTarget(self, action: #selector(actionHandleAppleSignin), for: .touchUpInside)
self.view.addSubview(customAppleLoginBtn)
// Setup Layout Constraints to be in the center of the screen
customAppleLoginBtn.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
customAppleLoginBtn.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
customAppleLoginBtn.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
customAppleLoginBtn.widthAnchor.constraint(equalToConstant: 200),
customAppleLoginBtn.heightAnchor.constraint(equalToConstant: 40)
])
}
}
@objc func actionHandleAppleSignin() {
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.presentationContextProvider = self
authorizationController.performRequests()
}
}
import AuthenticationServices
extension LoginViewController: ASAuthorizationControllerPresentationContextProviding {
// For present window
@available(iOS 13.0, *)
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
return self.view.window!
}
}
extension LoginViewController: ASAuthorizationControllerDelegate {
// Authorization Failed
@available(iOS 13.0, *)
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
print(error.localizedDescription)
}
// Authorization Succeeded
@available(iOS 13.0, *)
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
// Get user data with Apple ID credentitial
let userId = appleIDCredential.user
let userFirstName = appleIDCredential.fullName?.givenName
let userLastName = appleIDCredential.fullName?.familyName
let userEmail = appleIDCredential.email
print("User ID: \(userId)")
print("User First Name: \(userFirstName ?? "")")
print("User Last Name: \(userLastName ?? "")")
print("User Email: \(userEmail ?? "")")
// Write your code here
} else if let passwordCredential = authorization.credential as? ASPasswordCredential {
// Get user data using an existing iCloud Keychain credential
let appleUsername = passwordCredential.user
let applePassword = passwordCredential.password
// Write your code here
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.