简体   繁体   English

如何在Objective C中使用wkWebView使用参数执行Javascript?

[英]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.

相关问题 使用 WKWebView Objective C 在 AngularJS 中形成 Post - Form Post in AngularJS using WKWebView Objective C 如何使用目标C或JavaScript在Cydget中运行系统命令? - How do I run a system command in a cydget using objective c or javascript? 使用 WKWebView 在 swiftui 中执行 javascript 确认 - Execute javascript confirm in swiftui using WKWebView 如何在 WKWebview 中执行 Javascript 函数? - How to execute a Javascript function inside WKWebview? 如何在 WKWebView Swift 中运行 javascript 命令 - How do I run a javascript command in WKWebView Swift WKWebView JavaScript将消息发送到Objective-C并返回值 - The WKWebView JavaScript sends the message to Objective-C and returns the value 如何从目标C中的stringByEvaluatingJavaScriptFromString:方法执行长JavaScript? - How to execute long javascript from stringByEvaluatingJavaScriptFromString: method in objective C? 如何使用evaluateJavaScript 将数据从WKWebview 发送到HTML 文件 | iOS | 目标-C - How to send data from WKWebview to HTML file using evaluateJavaScript | iOS | Objective-C 如何使用 selenium 在 python 中执行 JavaScript 代码? - How do I execute a JavaScript code in python using selenium? javascript如何在Webkit控制台执行数据? - How do I execute data in Webkit consoles by using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM