简体   繁体   中英

ASWebAuthenticationSession on iOS 13

I installed iOS 13, the authentication via Safari no longer works. I have the same configuration on iOS 12 except 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];

I find a solution

Add above the implementation.

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
@interface AppDelegate() <ASWebAuthenticationPresentationContextProviding>
@end
#endif

In your code for 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];

Add method

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
#pragma mark - ASWebAuthenticationPresentationContextProviding
- (ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:(ASWebAuthenticationSession *)session  API_AVAILABLE(ios(13.0)){
   return UIApplication.sharedApplication.keyWindow;
}
#endif

within UIScene sometimes it's hard to determine the right context and all samples above may not work as expected.

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
  }
}

then somewhere in u'r code:

  var contextProvider: AuthContextProvider?
  var session: NSObject?
  

and in func with auth call

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM