如果在项目中采用WKWebView的方法加载网页,OC向JS传值方法总结:
1、OC->JS 传数组的方法:
NSString *arrStr = [self.arr componentsJoinedByString:@","];
NSString *js = [NSString stringWithFormat:@"text('%ld',[%@]);",index,arrStr];
[self.wkView evaluateJavaScript:js
completionHandler:^(id _Nullable parm1, NSError * _Nullable error) {
NSLog(@"%@",[error description]);
}];
2、OC->JS 传字典的方法:
NSString *jscript = [NSString stringWithFormat:@"post('%@',{%@});", self.URLString, self.postData];
// 调用JS代码
[self.wkWebView evaluateJavaScript:jscript completionHandler:^(id object, NSError * _Nullable error) {
}];
postData请求块 注意格式:@"\"username\":\"xxxx\",\"password\":\"xxxx\""
1、OC->JS 传数组的方法:
NSString *arrStr = [self.arr componentsJoinedByString:@","];
NSString *js = [NSString stringWithFormat:@"text('%ld',[%@]);",index,arrStr];
[self.wkView evaluateJavaScript:js
completionHandler:^(id _Nullable parm1, NSError * _Nullable error) {
NSLog(@"%@",[error description]);
}];
2、OC->JS 传字典的方法:
NSString *jscript = [NSString stringWithFormat:@"post('%@',{%@});", self.URLString, self.postData];
// 调用JS代码
[self.wkWebView evaluateJavaScript:jscript completionHandler:^(id object, NSError * _Nullable error) {
}];
postData请求块 注意格式:@"\"username\":\"xxxx\",\"password\":\"xxxx\""