ios - iOS 13 上的 ASWebAuthenticationSession
问题描述
我安装了 iOS 13,无法通过 Safari 进行身份验证。我在 iOS 12 上有相同的配置,除了self.authSessionAS.presentationContextProvider = self;
self.authSessionAS = [[ASWebAuthenticationSession alloc]initWithURL:[[NSURL alloc] initWithString:self.authUrl] callbackURLScheme:@"app://" completionHandler:^(NSURL * _Nullable callbackURL, NSError * _Nullable error) {
if(callbackURL)
{
self.resultStream(callbackURL.absoluteString);
}else
{
self.resultStream(@"");
}
self.resultStream = NULL;
}];
self.authSessionAS.presentationContextProvider = self;
[self.authSessionAS start];
解决方案
我找到了解决方案
添加上面的实现。
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
@interface AppDelegate() <ASWebAuthenticationPresentationContextProviding>
@end
#endif
在您的 Auth 代码中。
self.authSessionAS = [[ASWebAuthenticationSession alloc]initWithURL:[[NSURL alloc] initWithString:self.authUrl] callbackURLScheme:@"app://" completionHandler:^(NSURL * _Nullable callbackURL, NSError * _Nullable error) {
if(callbackURL)
{
self.resultStream(callbackURL.absoluteString);
}else
{
self.resultStream(@"");
}
self.resultStream = NULL;
}];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
if (@available(iOS 13, *)) {
self.authSessionAS.presentationContextProvider = self;
}
#endif
[self.authSessionAS start];
添加方法
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
#pragma mark - ASWebAuthenticationPresentationContextProviding
- (ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:(ASWebAuthenticationSession *)session API_AVAILABLE(ios(13.0)){
return UIApplication.sharedApplication.keyWindow;
}
#endif