我需要更改一个函数来评估从UIWebView到WKWebView的JavaScript。我需要在这个函数中返回求值结果。
现在,我正在打电话:
[wkWebView evaluateJavaScript:call completionHandler:^(NSString *result, NSError *error)
{
NSLog(@"Error %@",error);
NSLog(@"Result %@",result);
}];
但是我需要得到像返回值这样的结果,就像在UIWebView
中一样。
你能建议一个解决方案吗?
答案 0 :(得分:30)
更新:这不再适用于iOS 12+。
我通过等待结果直到返回结果值来解决了这个问题。
我使用NSRunLoop等待,但我不确定它的最佳方式......
以下是我现在使用的类别扩展源代码:
@interface WKWebView(SynchronousEvaluateJavaScript)
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;
@end
@implementation WKWebView(SynchronousEvaluateJavaScript)
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
{
__block NSString *resultString = nil;
__block BOOL finished = NO;
[self evaluateJavaScript:script completionHandler:^(id result, NSError *error) {
if (error == nil) {
if (result != nil) {
resultString = [NSString stringWithFormat:@"%@", result];
}
} else {
NSLog(@"evaluateJavaScript error : %@", error.localizedDescription);
}
finished = YES;
}];
while (!finished)
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
return resultString;
}
@end
示例代码:
NSString *userAgent = [_webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSLog(@"userAgent: %@", userAgent);
答案 1 :(得分:18)
如果javascript的代码引发NSError:
,此解决方案也有效- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script {
__block NSString *resultString = nil;
__block BOOL finished = NO;
[self evaluateJavaScript:script completionHandler:^(id result, NSError *error) {
if (error == nil) {
if (result != nil) {
resultString = [NSString stringWithFormat:@"%@", result];
}
} else {
NSLog(@"evaluateJavaScript error : %@", error.localizedDescription);
}
finished = YES;
}];
while (!finished)
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
return resultString;
}
答案 2 :(得分:12)
我只是偶然发现了同样的问题并为它写了一个小的Swift(3.0)WKWebView扩展,我想可以分享它:
extension WKWebView {
func evaluate(script: String, completion: (result: AnyObject?, error: NSError?) -> Void) {
var finished = false
evaluateJavaScript(script) { (result, error) in
if error == nil {
if result != nil {
completion(result: result, error: nil)
}
} else {
completion(result: nil, error: error)
}
finished = true
}
while !finished {
RunLoop.current().run(mode: .defaultRunLoopMode, before: Date.distantFuture)
}
}
}
答案 3 :(得分:6)
基于@ mort3m的答案,这是可与Swift 5一起使用的WKWebView扩展。
extension WKWebView {
func evaluate(script: String, completion: @escaping (Any?, Error?) -> Void) {
var finished = false
evaluateJavaScript(script, completionHandler: { (result, error) in
if error == nil {
if result != nil {
completion(result, nil)
}
} else {
completion(nil, error)
}
finished = true
})
while !finished {
RunLoop.current.run(mode: RunLoop.Mode(rawValue: "NSDefaultRunLoopMode"), before: NSDate.distantFuture)
}
}
}
答案 4 :(得分:3)
我发现注入的javascript中final语句的值是作为完成函数的id参数传递的返回值,如果没有异常的话。所以,例如:
static MarvinImage memory = new MarvinImage();
答案 5 :(得分:0)
仅此有效,以上答案对我无效。
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
{
__block NSString *resultString = nil;
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
[self evaluateJavaScript:script completionHandler:^(id result, NSError *error) {
if (error == nil) {
if (result != nil) {
resultString = [NSString stringWithFormat:@"%@", result];
}
} else {
NSLog(@"evaluateJavaScript error : %@", error.localizedDescription);
}
dispatch_semaphore_signal(sem);
}];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
return resultString;
}
答案 6 :(得分:-1)
可以使用调度信号量。它适用于 iOS12 +
示例:
__block NSString *resultString = nil;
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
[self evaluateJavaScript:script completionHandler:^(id result, NSError *error) {
if (error == nil) {
if (result != nil) {
resultString = [NSString stringWithFormat:@"%@", result];
dispatch_semaphore_signal(sem);
}
} else {
NSLog(@"evaluateJavaScript error : %@", error.localizedDescription);
}
finished = YES;
}];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
//process resultString here.