接入一个第四方支付时,支付那边必须用form表单提交数据,导致需要我们App端加载HTML代码显示出网页支付操作,遇到了坑,所以记录一下:
1.获取到后台返回的form表单,并拼接HTML代码
- (NSString *)getHtmlStringWithData:(id)data
{
NSMutableString *html = [NSMutableString string];
[html appendString:@"<html>"];
[html appendString:@"<head>"];
//加载css文件
[html appendFormat:@"<link rel=\"stylesheet\" href=\"%@\">",[[NSBundle mainBundle] URLForResource:@"mipay" withExtension:@"css"]];
//禁止wkwebview缩放
[html appendString:@"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1,user-scalable=no\">"];
[html appendString:@"</head>"];
[html appendString:@"<body>"];
NSString *form = [data objectForKey:@"html"];
[html appendString:form];
[html appendString:@"</body>"];
[html appendString:@"</html>"];
return html;
}
2.WKWebView加载HTML代码
//获取css资源文件,file:///Resources/是路径
NSURL *baseUrl = [NSURL URLWithString:@"file:///Resources/"];
[_wkwebView loadHTMLString:self.htmlStr baseURL:baseUrl];
css文件所在路径如图本来到此结束,不过遇到两个问题
3.WKWebView显示的网页内容被缩放,禁止缩放的方法已经放到获取HTML代码里,查看步骤1