[iOS] WKWebView 於JavaScript傳值
阿新 • • 發佈:2018-12-03
如果在專案中採用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\""