方法一:
使用js的form表單發(fā)送post請(qǐng)求
首先定義js函數(shù)的字符串宏
#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();\
}"
執(zhí)行使用
NSString *jsString = [NSString stringWithFormat:@"%@wkwebview_post(\"%@\", \"%@\", %@)",KWKWebViewPost_JS,@"https://www.baidu.com",@"application/x-www-form-urlencoded",@""];
[webview evaluateJavaScript:jsString completionHandler:nil];
// 測(cè)試js的html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鳥(niǎo)教程(runoob.com)</title>
</head>
<body>
<p>來(lái)調(diào)用帶參數(shù)的函數(shù)。</p>
<button onclick='wkwebview_post("http://baidu.com", "application/x-www-form-urlencoded", )'>點(diǎn)擊這里</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發(fā)送post請(qǐng)求到服務(wù)器
2.獲取服務(wù)器返回的HTML數(shù)據(jù),使用WKWebview加載