| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- // Copyright 2019 Google
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
- #import "FIRAppDistributionAppDelegateInterceptor.h"
- #import "FIRFADLogger+Private.h"
- #import <AuthenticationServices/AuthenticationServices.h>
- #import <SafariServices/SafariServices.h>
- #import <UIKit/UIKit.h>
- @implementation FIRAppDistributionAppDelegateInterceptor
- API_AVAILABLE(ios(9.0))
- SFSafariViewController *_safariVC;
- API_AVAILABLE(ios(12.0))
- ASWebAuthenticationSession *_webAuthenticationVC;
- API_AVAILABLE(ios(11.0))
- SFAuthenticationSession *_safariAuthenticationVC;
- - (instancetype)init {
- self = [super init];
- self.safariHostingViewController = [[UIViewController alloc] init];
- return self;
- }
- + (instancetype)sharedInstance {
- static dispatch_once_t once;
- static FIRAppDistributionAppDelegateInterceptor *sharedInstance;
- dispatch_once(&once, ^{
- sharedInstance = [[FIRAppDistributionAppDelegateInterceptor alloc] init];
- });
- return sharedInstance;
- }
- - (void)appDistributionRegistrationFlow:(NSURL *)URL
- withCompletion:(void (^)(NSError *_Nullable error))completion {
- NSString *callbackURL = [NSString stringWithFormat:@"com.firebase.appdistribution.%@", [[FIRApp defaultApp] options].googleAppID];
- FIRFADInfoLog(@"Registration URL: %@", URL);
- FIRFADInfoLog(@"Callback URL: %@", callbackURL);
- if (@available(iOS 12.0, *)) {
- ASWebAuthenticationSession *authenticationVC = [[ASWebAuthenticationSession alloc]
- initWithURL:URL
- callbackURLScheme:callbackURL
- completionHandler:^(NSURL *_Nullable callbackURL, NSError *_Nullable error) {
- [self resetUIState];
- FIRFADInfoLog(@"Sign in complete using ASWebAuthenticationSession");
- // TODO (b/161538029): Map these errors to AppDistribution error codes
- completion(error);
- }];
- if (@available(iOS 13.0, *)) {
- authenticationVC.presentationContextProvider = self;
- }
- _webAuthenticationVC = authenticationVC;
- [authenticationVC start];
- } else if (@available(iOS 11.0, *)) {
- _safariAuthenticationVC = [[SFAuthenticationSession alloc]
- initWithURL:URL
- callbackURLScheme:callbackURL
- completionHandler:^(NSURL *_Nullable callbackURL, NSError *_Nullable error) {
- [self resetUIState];
- FIRFADInfoLog(@"Sign in complete using SFAuthenticationSession");
- // TODO (b/161538029): Map these errors to AppDistribution error codes
- completion(error);
- }];
- [_safariAuthenticationVC start];
- } else if (@available(iOS 9.0, *)){
- SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:URL];
- safariVC.delegate = self;
- _safariVC = safariVC;
- [self->_safariHostingViewController presentViewController:safariVC animated:YES completion:nil];
- self.registrationFlowCompletion = completion;
- }
- }
- - (void)showUIAlert:(UIAlertController *)alertController {
- [self initializeUIState];
- [self.window.rootViewController presentViewController:alertController
- animated:YES
- completion:nil];
- }
- - (BOOL)application:(UIApplication *)application
- openURL:(NSURL *)URL
- options:(NSDictionary<NSString *, id> *)options {
- [self registrationFlowCompletion];
- [self resetUIState];
- return NO;
- }
- - (void)initializeUIState {
- if (self.window) {
- return;
- }
- // Create an empty window + viewController to host the Safari UI.
- self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
- self.window.rootViewController = self.safariHostingViewController;
- // Place it at the highest level within the stack.
- self.window.windowLevel = +CGFLOAT_MAX;
- // Run it.
- [self.window makeKeyAndVisible];
- }
- - (void)resetUIState {
- if (self.window) {
- self.window.hidden = YES;
- self.window = nil;
- }
- self.registrationFlowCompletion = nil;
- if (@available(iOS 11.0, *)) {
- _safariAuthenticationVC = nil;
- } else if (@available(iOS 12.0, *)) {
- _webAuthenticationVC = nil;
- } else if (@available(iOS 9.0, *)) {
- _safariVC = nil;
- }
- }
- - (void)safariViewControllerDidFinish:(SFSafariViewController *)controller NS_AVAILABLE_IOS(9.0) {
- [self resetUIState];
- }
- - (ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:
- (ASWebAuthenticationSession *)session API_AVAILABLE(ios(13.0)) {
- return self.safariHostingViewController.view.window;
- }
- @end
|