【问题标题】:WKWebView throwing HTTP ERROR 401- Authorization Failed in Cocoa(Mac application) in swiftWKWebView 在 Swift 中抛出 HTTP ERROR 401-Authorization Failed in Cocoa(Mac application)
【发布时间】:2020-10-02 08:02:21
【问题描述】:

我在 Mac 应用程序中打开 webApplication,打开 webApplication 我使用 webView.load 方法在 webView 中显示内容。大多数时候能够加载内容,但有时会抛出授权失败错误。这是一个间歇性问题。

func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse,
                 decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {

        if let response = navigationResponse.response as? HTTPURLResponse {
            print("Response: \(response)")
            
            if response.statusCode == 401 {

使用 AppleConnect 单点登录的 Web 应用程序。

func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        if let serverTrust = challenge.protectionSpace.serverTrust {
            completionHandler(URLSession.AuthChallengeDisposition.performDefaultHandling, URLCredential(trust: serverTrust))
            //completionHandler(URLSession.AuthChallengeDisposition.performDefaultHandling, nil)
            
        }
    }

此错误仅在 Big Sur 操作系统中出现,而不是在 Catalina 中,这是一个不连续的间歇性问题。以前有人遇到过这个问题吗?

【问题讨论】:

    标签: swift cocoa cocoa-touch wkwebview http-status-code-401


    【解决方案1】:

    为了解决授权失败错误,我添加了非持久性代码

    let webViewConfig = WKWebViewConfiguration()
    webView = WKWebView(frame: view.frame, configuration: webViewConfig)
    

    /** @abstract 返回一个新的非持久性数据存储。 @discussion 如果 WKWebView 与非持久数据存储相关联,则不会有任何数据 写入文件系统。这对于在 Web 视图中实现“隐私浏览”很有用。 */

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-19
      • 1970-01-01
      • 2020-02-18
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2020-06-07
      相关资源
      最近更新 更多