[英]How do I execute Javascript with parameters using wkWebView in Objective C?
I am currently trying to use: 我目前正在尝试使用:
NSString* arg = [NSString stringWithFormat:@"ReadiumSDK.reader.openSpineItemElementCfi(%@,%@,)", val1,val2];
[self executeJavaScript:arg completionHandler:nil];
but this throws an error. 但这会引发错误。
I pass parameters into javascript functions all the time from a WkWebView: 我一直从WkWebView将参数传递到javascript函数中:
One problem may be that your parameters need to be inside single quotes and you have an extra , at the end: "ReadiumSDK.reader.openSpineItemElementCfi('%@','%@')" 一个问题可能是您的参数需要放在单引号内,并且最后还有一个额外的:“ ReadiumSDK.reader.openSpineItemElementCfi('%@','%@')”
The key is calling the function the exact same way you would in a developer console in a browser: 关键是调用该函数的方式与在浏览器的开发人员控制台中完全相同:
Sample below in Chrome's developer tools I would type: SomeFunction('stringParameter') 我将在Chrome开发人员工具中键入以下示例:SomeFunction('stringParameter')
NSString *someParameter = @"stringParameter";
NSString *javascript = [NSString stringWithFormat:@"SomeFunction('%@')", someParameter];
[wkWebView evaluateJavaScript:javascript completionHandler:^(NSString *result, NSError *error) {
if(error != nil) {
NSLog(@"SomeFunction Error: %@",error);
return;
}
NSLog(@" SomeFunction Success");
}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.