问题描述
我正在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 singleSignOn的WebApplication。
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 OS上出现,在Catalina中不出现,并且这是一个间歇性问题,不连续。有人遇到过这个问题吗?
解决方法
要解决授权失败错误,我添加了非永久性代码
let webViewConfig = WKWebViewConfiguration()
webView = WKWebView(frame: view.frame,configuration: webViewConfig)
/ ** @abstract返回一个新的非持久数据存储。 @discussion如果WKWebView与非持久数据存储相关联,则不会有任何数据 写入文件系统。这对于在Web视图中实现“私人浏览”很有用。 * /