我安装了Safari13,通过iOS的身份验证不再起作用。除了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];发布于 2019-11-08 19:23:17
我找到了一个解决方案
添加到实现之上。
#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];Add方法
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
#pragma mark - ASWebAuthenticationPresentationContextProviding
- (ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:(ASWebAuthenticationSession *)session API_AVAILABLE(ios(13.0)){
return UIApplication.sharedApplication.keyWindow;
}
#endif发布于 2020-07-28 13:42:09
在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?并在函数中使用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()https://stackoverflow.com/questions/58237404
复制相似问题