Skip to content

Instantly share code, notes, and snippets.

@anupamchugh
Last active July 11, 2023 20:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anupamchugh/80e77399abceb25a1fec936a973b1a68 to your computer and use it in GitHub Desktop.
Save anupamchugh/80e77399abceb25a1fec936a973b1a68 to your computer and use it in GitHub Desktop.
class Coordinator: NSObject, ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding {
let parent: SignInWithAppleView?
init(_ parent: SignInWithAppleView) {
self.parent = parent
super.init()
}
@objc func didTapButton() {
let appleIDProvider = ASAuthorizationAppleIDProvider()
let request = appleIDProvider.createRequest()
request.requestedScopes = [.fullName, .email]
let authorizationController = ASAuthorizationController(authorizationRequests: [request])
authorizationController.presentationContextProvider = self
authorizationController.delegate = self
authorizationController.performRequests()
}
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
let vc = UIApplication.shared.windows.last?.rootViewController
return (vc?.view.window!)!
}
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
guard let credentials = authorization.credential as? ASAuthorizationAppleIDCredential else {
print("credentials not found....")
return
}
let defaults = UserDefaults.standard
defaults.set(credentials.user, forKey: "userId")
parent?.name = "\(credentials.fullName?.givenName ?? "")"
}
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment