Skip to content

Commit 931255b

Browse files
authored
shared application fix (#11554)
* shared application fix * lint fix
1 parent 5bcc4fe commit 931255b

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

FirebaseAuth/Sources/MultiFactor/TOTP/FIRTOTPSecret.m

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#import <TargetConditionals.h>
1717
#if TARGET_OS_IOS
1818

19+
#import <GoogleUtilities/GULAppEnvironmentUtil.h>
1920
#import <UIKit/UIKit.h>
2021
#import "FirebaseAuth/Sources/Auth/FIRAuth_Internal.h"
2122
#import "FirebaseAuth/Sources/MultiFactor/TOTP/FIRTOTPSecret+Internal.h"
@@ -60,10 +61,28 @@ - (NSString *)generateQRCodeURLWithAccountName:(NSString *)accountName issuer:(N
6061

6162
- (void)openInOTPAppWithQRCodeURL:(NSString *)QRCodeURL {
6263
NSURL *url = [NSURL URLWithString:QRCodeURL];
63-
if ([[UIApplication sharedApplication] canOpenURL:url]) {
64-
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
64+
static Class applicationClass = nil;
65+
// iOS App extensions should not call [UIApplication sharedApplication], even if UIApplication
66+
// responds to it.
67+
if (![GULAppEnvironmentUtil isAppExtension]) {
68+
Class cls = NSClassFromString(@"UIApplication");
69+
if (cls && [cls respondsToSelector:@selector(sharedApplication)]) {
70+
applicationClass = cls;
71+
}
72+
}
73+
UIApplication *application = [applicationClass sharedApplication];
74+
if (application) {
75+
if ([application respondsToSelector:@selector(canOpenURL:)]) {
76+
if ([application canOpenURL:url]) {
77+
[application openURL:url options:@{} completionHandler:nil];
78+
} else {
79+
NSLog(@"URL cannot be opened");
80+
}
81+
} else {
82+
NSLog(@"Cannot access canOpenURL: method");
83+
}
6584
} else {
66-
FIRLogError(kFIRLoggerAuth, @"I-AUT000019", @"URL cannot be opened");
85+
NSLog(@"sharedApplication cannot be accessed");
6786
}
6887
}
6988

0 commit comments

Comments
 (0)