WKWebView evaluate JavaScript return value

后端 未结 6 1384
孤独总比滥情好
孤独总比滥情好 2020-12-04 09:33

I need to change a function to evaluate JavaScript from UIWebView to WKWebView. I need to return result of evaluating in this function.

Now, I am calling:



        
6条回答
  •  伪装坚强ぢ
    2020-12-04 10:10

    I've found that the value of final statement in your injected javascript is the return value passed as the id argument to the completion function, if there are no exceptions. So, for example:

    [self.webview evaluateJavaScript:@"var foo = 1; foo + 1;" completionHandler:^(id result, NSError *error) {
        if (error == nil)
        {
            if (result != nil)
            {
                NSInteger integerResult = [result integerValue]; // 2
                NSLog(@"result: %d", integerResult);
            }
        }
        else
        {
            NSLog(@"evaluateJavaScript error : %@", error.localizedDescription);
        }
    }];
    

提交回复
热议问题