Last active
July 11, 2023 20:57
-
-
Save anupamchugh/80e77399abceb25a1fec936a973b1a68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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