1.加載的html實(shí)際高度與展示出的高度不符(使用UIWebView時(shí)正常)
在頭中加入
<head>
<meta name="viewport" content="width=device-width,user-scalable=no, initial-scale=1">
</head>
2.參數(shù)太長(zhǎng)使用POST加載網(wǎng)頁(yè)時(shí)失敗
使用下方代碼請(qǐng)求失敗
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
暫未找到原因唠梨,替代方案為加載一段含有js請(qǐng)求的html厌杜,在加載完成后調(diào)用post方法
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<script type="text/javascript">
function post(path,json){
var method = "post";
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", "json");
hiddenField.setAttribute("value", json);
form.appendChild(hiddenField);
document.body.appendChild(form);
form.submit();
}
</script>
</html>
3.使用URLRequest在請(qǐng)求頭中加入cookie值無(wú)效
WKWebView不會(huì)再?gòu)腘SHTTPCookieStorage取cookie洁桌,而是需要在初始化時(shí)通過(guò)WKUserScript寫(xiě)入
WKUserContentController* userContentController = WKUserContentController.new;
NSString *scriptStr = [NSString stringWithFormat:@"document.cookie = 'JSESSIONID=%@'",[IMClient shareClient].sessionId];
WKUserScript * cookieScript = [[WKUserScript alloc]
initWithSource:scriptStr
injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
[userContentController addUserScript:cookieScript];
WKWebViewConfiguration* webViewConfig = WKWebViewConfiguration.new;
webViewConfig.userContentController = userContentController;
[webViewConfig.userContentController addScriptMessageHandler:self name:@"nativeInteraction"];
WKWebView *webView = [[WKWebView alloc]initWithFrame:self.view.bounds configuration:webViewConfig];
webView.scrollView.bounces = false;
webView.navigationDelegate = self;
[self.view addSubview:webView];
_webView = webView;
4.加載本地html時(shí)咳胃,css與圖片無(wú)效
通常css及圖片引用如下
<link href="css/login.css" rel="stylesheet" type="text/css" />
background: url("../images/dc-bg.png") no-repeat;
去掉文件夾路徑笋庄, 更改為下方格式即可
<link href="login.css" rel="stylesheet" type="text/css" />
background: url("dc-bg.png") no-repeat;