Forked from iamchiwon/ASAuthorizationControllerProxy.swift
Created
July 21, 2021 01:56
-
-
Save stud214/43cb1e8a0bf4fc97d7b20ae4ded82b5e to your computer and use it in GitHub Desktop.
Sign in with Apple + Rx
This file contains hidden or 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
| import AuthenticationServices | |
| import RxCocoa | |
| import RxSwift | |
| import UIKit | |
| @available(iOS 13.0, *) | |
| extension Reactive where Base: ASAuthorizationAppleIDProvider { | |
| public func login(scope: [ASAuthorization.Scope]? = nil, on window: UIWindow) -> Observable<ASAuthorization> { | |
| let request = base.createRequest() | |
| request.requestedScopes = scope | |
| let controller = ASAuthorizationController(authorizationRequests: [request]) | |
| let proxy = ASAuthorizationControllerProxy.proxy(for: controller) | |
| proxy.presentationWindow = window | |
| controller.presentationContextProvider = proxy | |
| controller.performRequests() | |
| return proxy.didComplete | |
| } | |
| } | |
| @available(iOS 13.0, *) | |
| extension Reactive where Base: ASAuthorizationAppleIDButton { | |
| public func loginOnTap(scope: [ASAuthorization.Scope]? = nil) -> Observable<ASAuthorization> { | |
| let window = base.window! | |
| return controlEvent(.touchUpInside) | |
| .flatMap { | |
| ASAuthorizationAppleIDProvider().rx.login(scope: scope, on: window) | |
| } | |
| } | |
| public func login(scope: [ASAuthorization.Scope]? = nil) -> Observable<ASAuthorization> { | |
| return ASAuthorizationAppleIDProvider().rx.login(scope: scope, on: base.window!) | |
| } | |
| } |
This file contains hidden or 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
| import AuthenticationServices | |
| let appleLoginButton = ASAuthorizationAppleIDButton() | |
| //... | |
| appleLoginButton.rx | |
| .loginOnTap(scope: [.fullName, .email]) | |
| .subscribe(onNext: { result in | |
| guard let auth = result.credential as? ASAuthorizationAppleIDCredential else { return } | |
| print(auth.user) | |
| print(auth.email) | |
| print(auth.fullName?.givenName) | |
| print(auth.fullName?.familyName) | |
| }) | |
| .disposed(by: disposeBag) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment