【问题标题】:ASWebAuthenticationSession on iOS 13iOS 13 上的 ASWebAuthenticationSession
【发布时间】:2019-10-04 13:28:25
【问题描述】:

我安装了 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];

【问题讨论】:

  • 同意。有一些在 iOS 12 和 13 上运行的代码......使用 iOS 12,它可以正确检测 cookie 并使用它来预填充登录屏幕。在 iOS 13 中,它不会检测 cookie 并显示默认登录屏幕。大概是 iOS 13 中的一个错误...
  • 附录...如果应用程序在 Xcode 10(iOS 12 版本)中编译,则 ASWebAuthenticationSession 广泛适用于 iOS 13(尽管它不会获取 cookie)。
  • 与 Xcode 11 相比,ASWebAuthenticationSession 根本无法在 iOS 13 上运行——完全损坏。不知道如果在 Xcode 11 上编译并在 iOS 12 上运行会发生什么。
  • 我找到了这个解决方案dev.to/robotsquidward/…,但是我不知道如何在objective-c和flutter中实现它。
  • flutter 开发者请查看pub.dev/packages/flutter_web_auth

标签: ios objective-c flutter


【解决方案1】:

我找到了解决办法

在实现上面添加。

#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

【讨论】:

  • 使用#if __IPHONE_OS_VERSION_MAX_ALLOWED &gt;= 130000if #available(iOS 13.0, *)有什么区别?
【解决方案2】:

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?
  

在 func 中调用 auth

  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()

【讨论】:

    猜你喜欢
    • 2020-03-16
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    相关资源
    最近更新 更多