方法一:
使用js的form表单发送post请求
首先定义js函数的字符串宏
#define KWKWebViewPost_JS @"function wkwebview_post(path, enctype, params) {\
var method = \"POST\";\
var form = document.createElement(\"form\");\
form.setAttribute(\"method\", method);\
form.setAttribute(\"action\", path);\
form.setAttribute(\"enctype\", enctype);\
for(var key in params){\
if (params.hasOwnProperty(key)) {\
var hiddenFild = document.createElement(\"input\");\
hiddenFild.setAttribute(\"type\", \"hidden\");\
hiddenFild.setAttribute(\"name\", key);\
hiddenFild.setAttribute(\"value\", params[key]);\
}\
form.appendChild(hiddenFild);\
}\
document.body.appendChild(form);\
form.submit();\
}"
执行使用
NSString *jsString = [NSString stringWithFormat:@"%@wkwebview_post(\"%@\", \"%@\", %@)",KWKWebViewPost_JS,@"https://www.baidu.com",@"application/x-www-form-urlencoded",@""];
[webview evaluateJavaScript:jsString completionHandler:nil];
// 测试js的html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<p>来调用带参数的函数。</p>
<button onclick='wkwebview_post("http://baidu.com", "application/x-www-form-urlencoded", )'>点击这里</button>
<script>
function wkwebview_post(path, enctype, params)
{var method = "POST";
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);
form.setAttribute("enctype", enctype);
for(var key in params){
if (params.hasOwnProperty(key)) {
var hiddenFild = document.createElement("input");
hiddenFild.setAttribute("type", "hidden");
hiddenFild.setAttribute("name", key);
hiddenFild.setAttribute("value", params[key]);
}
form.appendChild(hiddenFild);
}
document.body.appendChild(form);form.submit();
}
</script>
</body>
</html>
方法二:
1.使用原生或AFNetworking发送post请求到服务器
2.获取服务器返回的HTML数据,使用WKWebview加载
网友评论