0
votes

Previously we are using webview to load local HTML file and evaluate the Json data on it which was working fine but now we are using WKWebview and loading the local HTML file and Evaluating Json data on it but it's loads very slowly comparing to Webview.

Code Snippet for WebView:

 NSURL *instructionsURL = [NSURL fileURLWithPath:path];                   
 NSURLRequest *req = [NSURLRequest requestWithURL:instructionsURL];
 [self.webViewHistory loadRequest:req];

NSString *js = [NSString stringWithFormat:@"activeDataDisplay(%@)", jsonActivityDataString];
[self.webViewHistory stringByEvaluatingJavaScriptFromString:js];

Code Snippet for WkWebview:

 NSURL *instructionsURL = [NSURL fileURLWithPath:path];
 NSURLRequest *req = [NSURLRequest requestWithURL:instructionsURL];
 [self.wkWebHistory loadRequest:req];

 NSString *js = [NSString stringWithFormat:@"activeDataDisplay(%@)", jsonActivityDataString];

[self.wkWebHistory evaluateJavaScript:js completionHandler:^(NSString *result, NSError *error) {
             NSLog(@“Completion”);
         }];

Any help or workaround for this? or is there any other way? Thanks in Adv.

1

1 Answers

0
votes

I found the answer by myself.

We need to evaluate the JS in

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
}

Instead of

- (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation {
}

The DOM is not ready while we are trying to evaluate it inside didCommitNavigation so the evaluateJavaScript was giving error.