在我的webView
委托方法中,我正在使用此代码检查webview
加载完全完成时的情况:
if ([[webView stringByEvaluatingJavaScriptFromString:@"document.readyState"] isEqualToString:@"complete"]) {
//Code to handle other things
//Sometimes after this block shouldStartLoadWithRequest is called
}
有时即使然后控制也会阻塞-shouldStartLoadWithRequest
正在调用此方法并且我的代码会中断。
他们还有其他选择吗?我也试过webview.isLoading
,但这也行不通。
答案 0 :(得分:0)
您可以使用javascript和userContentController作为页面的一种方式,以便回复您已加载并准备好的本机代码。设置WkWebView时,请按以下方式设置处理程序:
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKUserContentController *controller = [[WKUserContentController alloc] init];
[controller addScriptMessageHandler:self name:@"observe"];
theConfiguration.userContentController = controller;
CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
CGRect frame = self.view.frame;
frame.origin.y = statusBarFrame.size.height;
frame.size.height -= statusBarFrame.size.height;
self.primaryWebView = [[WKWebView alloc] initWithFrame:frame configuration:theConfiguration];
然后向ViewController添加一个名为userContentController的方法,如下所示:
- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Check the details in message.body for what your javascript said
NSLog(@"javascript message: %@", message.body);
});
}
答案 1 :(得分:0)
我找到了一种解决方法,可以在didFinishLoading之后停止进一步执行脚本。
我使用以下条件来停止reqeust:
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
if(navigationType == UIWebViewNavigationTypeLinkClicked)
{
//my code
}
}