SwiftUI WkwebView UIViewRepresentable 被调用两次或更多次并得到错误 NSURLErrorDomain 错误 -999

问题描述 投票:0回答:1

此页面加载了两次。我尝试使用一些“isLoading”@State var 来拦截错误,但不起作用。我认为在我的设置中还有一些其他错误,也许是在我初始化 webView 的方式中,我认为它不应该在 struct 中初始化,而应该在 makeUIView 中初始化,但正如你所看到的,我需要将 webView 全局化。

我收到错误

操作无法完成。 (NSURLErrorDomain 错误-999)。

我这样调用代码:

let customWebView = PreferredAppsCustomWebView(url: url, canGoBack: $canGoBack, canGoForward: $canGoForward) { error in
                            self.errorMessage = error
                            self.showAlert = true
                        }
customWebView //not clear why this is required in order the element to work, but that is

网页视图的完整代码

import SwiftUI
import WebKit

struct PreferredAppsCustomWebView: UIViewRepresentable {

let url: URL
@Binding var canGoBack: Bool
@Binding var canGoForward: Bool

var webView = WKWebView()

var handleError: ((String) -> Void)?

//MARK: - UIViewRepresentable
func makeUIView(context: Context) -> WKWebView {
    webView.navigationDelegate = context.coordinator
    return webView
}

func updateUIView(_ uiView: WKWebView, context: Context) {
    let request = URLRequest(url: url)
    Logger.info("loading url: \(url)")
    uiView.load(request)
}


//MARK: - Delegate methods section
func makeCoordinator() -> Coordinator {
    Coordinator(self)
}

class Coordinator: NSObject, WKNavigationDelegate {
    
    var parent: PreferredAppsCustomWebView
    
    init(_ parent: PreferredAppsCustomWebView) {
        self.parent = parent
    }
    
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        DispatchQueue.main.async {
            self.parent.canGoBack = webView.canGoBack
            self.parent.canGoForward = webView.canGoForward
        }
    }
    
    
    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {

        Logger.error(error.localizedDescription)
        
        //this one is here only for info purposes.
        let nsError = error as NSError
        if nsError.code == NSURLErrorCancelled {
            Logger.error("maybe we are loading this webPage twice")
            return
        } else {
            //self.parent.handleError?(error.localizedDescription) //this one is necessary for propagating errors for webView.
        }
    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    }
    
}

func goBack() {
    if webView.canGoBack {
        Logger.info("ok")
        webView.goBack()
    } else {
        Logger.info("no")
    }
}

func goForward() {
    if webView.canGoForward {
        Logger.info("ok")
        webView.goForward()
    } else {
        Logger.info("no")
    }
}

func reload() {
    self.webView.reload()
}

}
swiftui wkwebview uiviewrepresentable nsurlerrordomain
1个回答
0
投票

如果其他正确的网址没有问题,那么我猜您的网址已重定向或存在其他问题。
要找到它,您可以检查

httpResponse

func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
    if let httpResponse = navigationResponse.response as? HTTPURLResponse {
        if httpResponse.statusCode >= 300 && httpResponse.statusCode < 400 {
            print("Redirected to: \(httpResponse.url?.absoluteString ?? "unknown")")
        } else {
            print("Not redirected")
        }
    }
    decisionHandler(.allow)
}
© www.soinside.com 2019 - 2024. All rights reserved.