I trigger a js from a WKWebView and get the result in the block function. In the block function, I got the correct data. However, when I assign this data to the external variable outside, I always get nil value.
- (id)triggerJS:(NSString*)js
{
NSLog(@"JS = %@",js);
__block id resultJS = nil;
[self.webView evaluateJavaScript:js completionHandler: ^(id data, NSError* err) {
resultJS = [data copy];
}
];
if (resultJS != nil)
{
NSString* str = (NSString*)resultJS;
NSLog(@"Result of JS = %@", str);
}
else
{
NSLog(@"Result of JS = nil");
}
return resultJS;
}
Why is that? Thanks.