iOS 13上的ASWebAuthenticationSession

3

我安装了iOS 13后,通过Safari进行身份验证的功能不再可用。 除了self.authSessionAS.presentationContextProvider = self;之外,我的iOS 12配置相同。

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];

1
同意。有些代码在iOS 12和13上都可以运行...在iOS 12上,它正确检测到cookie并使用它来填充登录屏幕。 在iOS 13上,它不能检测到cookie并呈现默认的登录屏幕。可能是iOS 13中的一个错误... - Richard
1
附加说明...如果应用程序在Xcode 10(iOS 12发布版)中编译,则ASWebAuthenticationSession在iOS 13上广泛适用(尽管它不会获取cookie)。 - Richard
1
与Xcode 11相比,在iOS 13上ASWebAuthenticationSession根本无法工作--完全失效。不知道如果在Xcode 11上编译并在iOS 12上运行会发生什么。 - Richard
1
我发现了这个解决方案 https://dev.to/robotsquidward/quick-guide-to-aswebauthenticationsession-api-changes-in-ios-13-4m8i,但我不知道如何在Objective-C和Flutter中实现它。 - lsaudon
对于Flutter开发者,请查看https://pub.dev/packages/flutter_web_auth。 - lsaudon
2个回答

3
我找到了一个解决方案。 在实现之前添加上述内容。
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
@interface AppDelegate() <ASWebAuthenticationPresentationContextProviding>
@end
#endif

在您的身份验证代码中。

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

使用 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000if #available(iOS 13.0, *) 有什么区别? - Turnipdabeets

-1

UIScene中,有时很难确定正确的上下文,所有上面的示例可能无法按预期工作。

import AuthenticationServices
import UIKit

public protocol AuthContextProvider where Self: ASWebAuthenticationPresentationContextProviding {

  func clear()
}

final class ContextProvider: NSObject, AuthContextProvider {

  private var context: ASPresentationAnchor?

  // MARK: - ASWebAuthenticationPresentationContextProviding

  public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
    let window = UIWindow()
    window.makeKeyAndVisible()
    self.context = window
    return window
  }

  public func clear() {
    context = nil
  }
}

然后在你的代码中的某个地方:

  var contextProvider: AuthContextProvider?
  var session: NSObject?
  

并在具有授权调用的函数中

  let session = ASWebAuthenticationSession(url: url, callbackURLScheme: callbackScheme) {
    url, error in

    if #available(iOS 13, *) {
      self.contextProvider?.clear() // clear context
    }

    completion(url, error)
  }

  self.session = session // retain session

  if #available(iOS 13, *) {
    self.contextProvider = ContextProvider() // retain context
    session.presentationContextProvider = self.contextProvider
  }

  session.start()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接