5

我正在创建一个ASWebAuthenticationSession会话,并且在完成处理程序中,当用户取消登录时,我有清理任务:

let session = ASWebAuthenticationSession(url: url, callbackURLScheme: redirectURI) { (callbackURL: URL?, error: Error?) in
    if case .ASWebAuthenticationSessionError.canceledLogin? = error {
        // clean up tasks
    }

    // proceed...
}

在 iOS 13+ 中,用户可以向下滑动以关闭,但在这种情况下根本不会触发整个完成处理程序。我不想通过启用 isModalInPresentation 来禁用此手势。

有没有办法让 ASWebAuthenticationSessionError.canceledLogin 在这种情况下触发,或者我如何检测用户向下滑动以取消ASWebAuthenticationSession会话?

4

2 回答 2

1

我有同样的问题。我发现,如果用户向下滑动屏幕ASWebAuthenticationSession,从中呈现 的 ViewController 会调用其函数。因此,如果您想对这种用户交互做出反应,据我所知,这是唯一可以做到这一点的地方。找出从哪个 ViewController呈现出来并覆盖它的功能。dismiss()ASWebAuthenticationSessionASWebAuthenticationSessiondismiss()

于 2020-11-10T16:54:45.653 回答
0

令人沮丧的是这是多么困难,但我能够通过覆盖窗口的willRemoveSubview().

class MyWindow: UIWindow {
    var willRemoveSubviewCallback: (() -> ())?

    override func willRemoveSubview(_ subview: UIView) {
        super.willRemoveSubview(subview)
        willRemoveSubviewCallback?()
    }
}

func signIn() {
    let url = ...
    let scheme = ...
    let window = UIApplication.shared.delegate!.window as! MyWindow
    
    var hasCalledHandler = false
    let handler: ASWebAuthenticationSession.CompletionHandler = { [weak window] (callbackUrl, error) in
        guard !hasCalledHandler else {
            return
        }
        hasCalledHandler = true
        window?.willRemoveSubviewCallback = nil
        
        // Handle callbackurl / error...
    }
    
    let session = ASWebAuthenticationSession(url: url, callbackURLScheme: scheme, completionHandler: handler)
    session.presentationContextProvider = window
    let success = session.start()
    if success {
        window.willRemoveSubviewCallback = {
            handler(nil, NSError(domain: ASWebAuthenticationSessionError.errorDomain, code: ASWebAuthenticationSessionError.canceledLogin.rawValue, userInfo: nil))
        }
    }
}

extension UIWindow: ASWebAuthenticationPresentationContextProviding {
    public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
        return self
    }
}
于 2021-03-08T06:55:08.650 回答