With recent major updates released by Apple on 3rd June 2019, there is one feature Sign In with Apple. Information about 'how to use this' in app is available but I can see any sample source code, how to achieve this feature in your existing iOS app.
I'm looking for a sample source code, as I can't understand how to start with this.
And what I've tried: Sign In with Apple
On your iPhone, iPad, iPod touch, or Apple WatchOpen the Settings app, then tap your name. Tap Password & Security. Tap Apps Using Apple ID.
About Sign in with Apple When you see a Sign in with Apple button on a participating app or website, it means you can set up an account using your Apple ID. No need to use a social media account, fill out forms, or choose another new password.
Choose Apple menu > System Preferences. Click Sign In. Enter your Apple ID and password. If prompted, enter the six-digit verification code sent to your trusted device or phone number and complete sign in.
To use Sign in with Apple, download and open an app that supports the feature. A few apps you can try are Adobe Reader, Airbnb, Dropbox, eBay, Groupon, Instacart, Kayak, The New York Times, Squarespace, Spotify, TikTok, and WordPress.
First step is you need to import AuthenticationServices
let appleIDProvider = ASAuthorizationAppleIDProvider()
appleIDProvider.getCredentialState(forUserID: KeychainItem.currentUserIdentifier) { (credentialState, error) in
switch credentialState {
case .authorized:
// The Apple ID credential is valid.
break
case .revoked:
// The Apple ID credential is revoked.
break
case .notFound:
// No credential was found, so show the sign-in UI.
default:
break
}
}
Step1
Prompts the user if an existing iCloud Keychain credential or Apple ID credential is found. implement ASAuthorizationControllerDelegate
to
func performExistingAccountSetupFlows() {
// Prepare requests for both Apple ID and password providers.
let requests = [ASAuthorizationAppleIDProvider().createRequest(),
ASAuthorizationPasswordProvider().createRequest()]
// Create an authorization controller with the given requests.
let authorizationController = ASAuthorizationController(authorizationRequests: requests)
authorizationController.delegate = self
authorizationController.presentationContextProvider = self
authorizationController.performRequests()
}
extension ViewController: ASAuthorizationControllerDelegate {
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
//here is credentials .
}
}
}
extension ViewController: ASAuthorizationControllerPresentationContextProviding {
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
return self.view.window!
}
}
Step2:
create button
let authorizationButton = ASAuthorizationAppleIDButton()
authorizationButton.addTarget(self, action: #selector(handleAuthorizationAppleIDButtonPress), for: .touchUpInside)
Step3
@objc
func handleAuthorizationAppleIDButtonPress() {
let appleIDProvider = ASAuthorizationAppleIDProvider()
let request = appleIDProvider.createRequest()
request.requestedScopes = [.fullName, .email]
let authorizationController = ASAuthorizationController(authorizationRequests: [request])
authorizationController.delegate = self
authorizationController.presentationContextProvider = self
authorizationController.performRequests()
}
Availability : iOS 13 or higher
Demo Application: A complete working demo application with keychain integration available on Github - https://github.com/developerinsider/Sign-In-with-Apple-Demo
Note: I will update answer with more and more useful information soon.
Hope it is helpful .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With