我正在尝试在 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
协议。
我需要做其他事情吗?
非常感谢任何帮助。