iOS WKWebView Javascript
Create sync version
UIWebView javascript method is sync type.
On the other hand, WKWebView evaluateJavaScript:completionHandler: is async type.
Actually, we can get the result from callback method.
But, sometimes need sync type, waiting UI etc…
Implement sync type
This is extension
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)javascript { __block NSString *res = nil; __block BOOL finish = NO; int count = 0; [self evaluateJavaScript:javascript completionHandler:^(NSString *result, NSError *error){ res = result; finish = YES; }]; while(!finish) { [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; if (count==10) { // Wait 1s finish = YES; } count++; } return res; }