Tip: WKWebView executeJavascript must evaluate to a string
Dec 22, 2017 0:26:32 GMT
egghead likes this
Post by jontara on Dec 22, 2017 0:26:32 GMT
If you are using WKWebView (not UIWebView), executeJavascript MUST evaluate to a string. Not nil, and (apparently) not some other type.
If not, you will get error messages in the log:
RhoWKWebView| WKWebView.evalulateJavaScript error : Javascript execution returned a result of an unsupported type
There doesn't SEEM to be any problem caused by this, other than getting an error message in the log.
However, as well, I am experiencing crashes which go away if I do not call evaluateJavascript at all. Making sure your script evaluates to a string does not make that go away, but does at least make the error messages go away.
This deserves a note in the documentation.
I had this small bit of code:
js = %Q|if (typeof($) === 'function') {$(document).trigger("#{type}", [#{json}])};|
executeJavascript js
that was getting those error messages. The error messages go away with:
js = %Q|if (typeof($) === 'function') {$(document).trigger("#{type}", [#{json}])}; '';|
executeJavascript js
For reference, the code in Rhodes that deals with this from RhoWKWebView.m:
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script {
__block NSString *resultString = nil;
__block BOOL finished = NO;
[webview evaluateJavaScript:script completionHandler:^(id result, NSError *error) {
if (error == nil) {
if (result != nil) {
resultString = [NSString stringWithFormat:@"%@", result];
}
} else {
RAWLOG_ERROR1("WKWebView.evaluateJavaScript error : %s", [[error localizedDescription] UTF8String]);
}
finished = YES;
}];
while (!finished)
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
return resultString;
}
If not, you will get error messages in the log:
RhoWKWebView| WKWebView.evalulateJavaScript error : Javascript execution returned a result of an unsupported type
There doesn't SEEM to be any problem caused by this, other than getting an error message in the log.
However, as well, I am experiencing crashes which go away if I do not call evaluateJavascript at all. Making sure your script evaluates to a string does not make that go away, but does at least make the error messages go away.
This deserves a note in the documentation.
I had this small bit of code:
js = %Q|if (typeof($) === 'function') {$(document).trigger("#{type}", [#{json}])};|
executeJavascript js
that was getting those error messages. The error messages go away with:
js = %Q|if (typeof($) === 'function') {$(document).trigger("#{type}", [#{json}])}; '';|
executeJavascript js
For reference, the code in Rhodes that deals with this from RhoWKWebView.m:
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script {
__block NSString *resultString = nil;
__block BOOL finished = NO;
[webview evaluateJavaScript:script completionHandler:^(id result, NSError *error) {
if (error == nil) {
if (result != nil) {
resultString = [NSString stringWithFormat:@"%@", result];
}
} else {
RAWLOG_ERROR1("WKWebView.evaluateJavaScript error : %s", [[error localizedDescription] UTF8String]);
}
finished = YES;
}];
while (!finished)
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
return resultString;
}