Skip to content

Commit e05582b

Browse files
authored
Update OIDExternalUserAgentIOS.m
1 parent 2781038 commit e05582b

File tree

1 file changed

+68
-74
lines changed

1 file changed

+68
-74
lines changed

Sources/AppAuth/iOS/OIDExternalUserAgentIOS.m

Lines changed: 68 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -98,86 +98,80 @@ - (BOOL)presentExternalUserAgentRequest:(id<OIDExternalUserAgentRequest>)request
9898
BOOL openedUserAgent = NO;
9999
NSURL *requestURL = [request externalUserAgentRequestURL];
100100

101-
// iOS 12 and later, use ASWebAuthenticationSession
102-
if (@available(iOS 12.0, *)) {
103-
// ASWebAuthenticationSession doesn't work with guided access (rdar://40809553)
104-
if (!UIAccessibilityIsGuidedAccessEnabled()) {
105-
__weak OIDExternalUserAgentIOS *weakSelf = self;
106-
NSString *redirectScheme = request.redirectScheme;
107-
ASWebAuthenticationSession *authenticationVC =
108-
[[ASWebAuthenticationSession alloc] initWithURL:requestURL
109-
callbackURLScheme:redirectScheme
110-
completionHandler:^(NSURL * _Nullable callbackURL,
111-
NSError * _Nullable error) {
112-
__strong OIDExternalUserAgentIOS *strongSelf = weakSelf;
113-
if (!strongSelf) {
114-
return;
115-
}
116-
strongSelf->_webAuthenticationVC = nil;
117-
if (callbackURL) {
118-
[strongSelf->_session resumeExternalUserAgentFlowWithURL:callbackURL];
119-
} else {
120-
NSError *safariError =
121-
[OIDErrorUtilities errorWithCode:OIDErrorCodeUserCanceledAuthorizationFlow
122-
underlyingError:error
123-
description:nil];
124-
[strongSelf->_session failExternalUserAgentFlowWithError:safariError];
125-
}
126-
}];
127-
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
128-
if (@available(iOS 13.0, *)) {
129-
authenticationVC.presentationContextProvider = self;
130-
authenticationVC.prefersEphemeralWebBrowserSession = _prefersEphemeralSession;
101+
// ASWebAuthenticationSession doesn't work with guided access (rdar://40809553)
102+
if (!UIAccessibilityIsGuidedAccessEnabled()) {
103+
__weak OIDExternalUserAgentIOS *weakSelf = self;
104+
NSString *redirectScheme = request.redirectScheme;
105+
ASWebAuthenticationSession *authenticationVC =
106+
[[ASWebAuthenticationSession alloc] initWithURL:requestURL
107+
callbackURLScheme:redirectScheme
108+
completionHandler:^(NSURL * _Nullable callbackURL,
109+
NSError * _Nullable error) {
110+
__strong OIDExternalUserAgentIOS *strongSelf = weakSelf;
111+
if (!strongSelf) {
112+
return;
131113
}
132-
#endif
133-
_webAuthenticationVC = authenticationVC;
134-
openedUserAgent = [authenticationVC start];
114+
strongSelf->_webAuthenticationVC = nil;
115+
if (callbackURL) {
116+
[strongSelf->_session resumeExternalUserAgentFlowWithURL:callbackURL];
117+
} else {
118+
NSError *safariError =
119+
[OIDErrorUtilities errorWithCode:OIDErrorCodeUserCanceledAuthorizationFlow
120+
underlyingError:error
121+
description:nil];
122+
[strongSelf->_session failExternalUserAgentFlowWithError:safariError];
123+
}
124+
}];
125+
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
126+
if (@available(iOS 13.0, *)) {
127+
authenticationVC.presentationContextProvider = self;
128+
authenticationVC.prefersEphemeralWebBrowserSession = _prefersEphemeralSession;
135129
}
130+
#endif
131+
_webAuthenticationVC = authenticationVC;
132+
openedUserAgent = [authenticationVC start];
136133
}
137-
// iOS 11, use SFAuthenticationSession
138-
if (@available(iOS 11.0, *)) {
139-
// SFAuthenticationSession doesn't work with guided access (rdar://40809553)
140-
if (!openedUserAgent && !UIAccessibilityIsGuidedAccessEnabled()) {
141-
__weak OIDExternalUserAgentIOS *weakSelf = self;
142-
NSString *redirectScheme = request.redirectScheme;
143-
SFAuthenticationSession *authenticationVC =
144-
[[SFAuthenticationSession alloc] initWithURL:requestURL
145-
callbackURLScheme:redirectScheme
146-
completionHandler:^(NSURL * _Nullable callbackURL,
147-
NSError * _Nullable error) {
148-
__strong OIDExternalUserAgentIOS *strongSelf = weakSelf;
149-
if (!strongSelf) {
150-
return;
151-
}
152-
strongSelf->_authenticationVC = nil;
153-
if (callbackURL) {
154-
[strongSelf->_session resumeExternalUserAgentFlowWithURL:callbackURL];
155-
} else {
156-
NSError *safariError =
157-
[OIDErrorUtilities errorWithCode:OIDErrorCodeUserCanceledAuthorizationFlow
158-
underlyingError:error
159-
description:@"User cancelled."];
160-
[strongSelf->_session failExternalUserAgentFlowWithError:safariError];
161-
}
162-
}];
163-
_authenticationVC = authenticationVC;
164-
openedUserAgent = [authenticationVC start];
165-
}
134+
135+
// SFAuthenticationSession doesn't work with guided access (rdar://40809553)
136+
if (!openedUserAgent && !UIAccessibilityIsGuidedAccessEnabled()) {
137+
__weak OIDExternalUserAgentIOS *weakSelf = self;
138+
NSString *redirectScheme = request.redirectScheme;
139+
SFAuthenticationSession *authenticationVC =
140+
[[SFAuthenticationSession alloc] initWithURL:requestURL
141+
callbackURLScheme:redirectScheme
142+
completionHandler:^(NSURL * _Nullable callbackURL,
143+
NSError * _Nullable error) {
144+
__strong OIDExternalUserAgentIOS *strongSelf = weakSelf;
145+
if (!strongSelf) {
146+
return;
147+
}
148+
strongSelf->_authenticationVC = nil;
149+
if (callbackURL) {
150+
[strongSelf->_session resumeExternalUserAgentFlowWithURL:callbackURL];
151+
} else {
152+
NSError *safariError =
153+
[OIDErrorUtilities errorWithCode:OIDErrorCodeUserCanceledAuthorizationFlow
154+
underlyingError:error
155+
description:@"User cancelled."];
156+
[strongSelf->_session failExternalUserAgentFlowWithError:safariError];
157+
}
158+
}];
159+
_authenticationVC = authenticationVC;
160+
openedUserAgent = [authenticationVC start];
166161
}
167-
// iOS 9 and 10, use SFSafariViewController
168-
if (@available(iOS 9.0, *)) {
169-
if (!openedUserAgent && _presentingViewController) {
170-
SFSafariViewController *safariVC =
171-
[[SFSafariViewController alloc] initWithURL:requestURL];
172-
safariVC.delegate = self;
173-
_safariVC = safariVC;
174-
[_presentingViewController presentViewController:safariVC animated:YES completion:nil];
175-
openedUserAgent = YES;
176-
}
162+
163+
if (!openedUserAgent && _presentingViewController) {
164+
SFSafariViewController *safariVC =
165+
[[SFSafariViewController alloc] initWithURL:requestURL];
166+
safariVC.delegate = self;
167+
_safariVC = safariVC;
168+
[_presentingViewController presentViewController:safariVC animated:YES completion:nil];
169+
openedUserAgent = YES;
177170
}
178-
// iOS 8 and earlier, use mobile Safari
171+
172+
// if none of the above worked, try to open the URL in Safari
179173
if (!openedUserAgent){
180-
openedUserAgent = [[UIApplication sharedApplication] openURL:requestURL];
174+
openedUserAgent = [[UIApplication sharedApplication] openURL:requestURL options:{} completionHandler:nil];
181175
}
182176

183177
if (!openedUserAgent) {

0 commit comments

Comments
 (0)