我正在尝试在WKWebView控件中加载网页而不将其作为子视图加载:
WKPreferences *preferences = [[WKPreferences alloc] init]; preferences.javaScriptEnabled = YES; WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; configuration.preferences = preferences; WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration]; webView.navigationDelegate = self; NSMutableuRLRequest *urlRequest = [NSMutableuRLRequest requestWithURL:url]; [urlRequest setValue:@"Safari" forHTTPHeaderField:@"User-agent"]; [webView loadRequest:urlRequest];
我正在尝试加载的网址是:
https://www.google.com/search?q=mustang
未调用以下WKNavigationDelegate方法:
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { [webView evaluateJavaScript:@"document.getElementsByTagName('html')[0].outerHTML" completionHandler:^(id result,NSError *error) { NSLog(@"Result %@",result); }]; }
我的NSObject类符合WKNavigationDelegate协议.
我还需要做点什么吗?
非常感谢任何帮助.
解决方法
加载相同的URL似乎对我有用.
请注意:在主帧加载完成后调用didFinishNavigation方法(这包括从主框架中存在的其他主机加载内容).因此,如果某些内容响应缓慢,则在解决之前不会调用didFinishNavigation方法.