Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift “WKWebView已无效”;UserInfo={NSLocalizedDescription=WKWebView已无效_Swift_Wkwebview - Fatal编程技术网

Swift “WKWebView已无效”;UserInfo={NSLocalizedDescription=WKWebView已无效

Swift “WKWebView已无效”;UserInfo={NSLocalizedDescription=WKWebView已无效,swift,wkwebview,Swift,Wkwebview,我正在尝试使用webView检索html。我不想在视图文件中编写webView代码,因此尝试在其他类中实现它。下面是我的代码 class BlobHelper: NSObject,WKNavigationDelegate{ let webView = WKWebView() func getLyrics(){ let url = URL(string: "https://Blob")! let request = URLRequest(ur

我正在尝试使用webView检索html。我不想在视图文件中编写webView代码,因此尝试在其他类中实现它。下面是我的代码

class BlobHelper: NSObject,WKNavigationDelegate{

    let webView = WKWebView()

    func getLyrics(){


        let url = URL(string: "https://Blob")!
        let request = URLRequest(url: url)
         webView.load(request)
        webView.evaluateJavaScript("document.getElementsByTagName('html')[0].innerHTML;", completionHandler: {(value,error)in
            print(value)
            print(error)
        })
    }
    func webView(_ webView: WKWebView,didFinish navigation: WKNavigation!) { print("loaded") }
当我执行代码时,我得到
可选(Error Domain=wkerrodomain code=3“WKWebView无效”UserInfo={NSLocalizedDescription=WKWebView无效})

我试过的 我能够使用static检索html,但无法在加载后调用didfish。
非常感谢您的帮助。

现在检查,希望您能找到答案。我尝试了您的答案,但是看起来好像没有调用didfish。(
webView.load(request)
)被调用,而您没有与委托人连接,但是我确实有
webView.navigationDelegate=self
NSAppTransportSecurity nsallowsrarbiryloads
class ViewController: UIViewController, WKNavigationDelegate {

var webView = WKWebView()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    let configuration = WKWebViewConfiguration()
    self.webView = WKWebView(frame: .zero, configuration: configuration)
    webView.navigationDelegate = self
    let url = URL(string: "https://stackoverflow.com")!
               let request = URLRequest(url: url)
               webView.load(request)
    
    self.view = webView
    
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    webView.evaluateJavaScript("document.getElementsByTagName('html')[0].innerHTML;", completionHandler: { (value,error)in
        
           print(value)
    })
    
}}