我从这种格式的API获得字符串响应
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form id="formRedirectToPayment" action="http://gate.666666pay.cn/cooperate/gateway.cgi" method="POST" target="_blank">
<input type="hidden" name="amount" value="12.00" />
<input type="hidden" name="clientIp" value="127.0.0.1" />
<input type="hidden" name="expireTime" value="" />
<input type="hidden" name="extra" value="payment" />
<input type="hidden" name="merId" value="2018020911011185" />
<input type="hidden" name="notifyUrl" value="http://fz.zf55555.com:8081/pfpay" />
<input type="hidden" name="service" value="TRADE.H5PAY" />
<input type="hidden" name="sign" value="004ABEFFA5D7458844AB4EAFD7C154BA" />
<input type="hidden" name="summary" value="payment" />
<input type="hidden" name="tradeDate" value="20180302" />
<input type="hidden" name="tradeNo" value="PfQW180302200400018366QP" />
<input type="hidden" name="typeId" value="3" />
<input type="hidden" name="version" value="1.0.0.0" />
</form>
</body>
<script type="text/javascript">
document.getElementById('formRedirectToPayment').submit();
</script>
</html>
以下是设置webview的代码。
override func viewDidLoad() {
super.viewDidLoad()
if let rootVC = navigationController?.viewControllers.first {
navigationController?.viewControllers = [rootVC, self]
}
setWebView()
}
func setWebView() {
webView.loadHTMLString(htmlString, baseURL: nil)
}
我只会得到一个白色背景的空webview。如果这不能在webview中工作,我想将html字符串加载到safari,但似乎不可能。
这里的问题是form target
,它是_blank
,通过实现WKUIDelegate方法并拦截请求新webView的链接来解决这个问题。
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if (navigationAction.targetFrame?.isMainFrame)! {
webView.load(navigationAction.request)
}
return nil
}
我能够使用WKWebView来实现这一点,但是,你仍然会得到一个白色的屏幕,因为你的所有表单字段都是隐藏的。
import UIKit
import WebKit
let htmlString = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head><body><form id=\"formRedirectToPayment\" action=\"http://gate.666666pay.cn/cooperate/gateway.cgi\" method=\"POST\" target=\"_blank\"><input type=\"hidden\" name=\"amount\" value=\"12.00\" /><input type=\"hidden\" name=\"clientIp\" value=\"127.0.0.1\" /><input type=\"hidden\" name=\"expireTime\" value=\"\" /><input type=\"hidden\" name=\"extra\" value=\"payment\" /><input type=\"hidden\" name=\"merId\" value=\"2018020911011185\" /><input type=\"hidden\" name=\"notifyUrl\" value=\"http://fz.zf55555.com:8081/pfpay\" /><input type=\"hidden\" name=\"service\" value=\"TRADE.H5PAY\" /><input type=\"hidden\" name=\"sign\" value=\"004ABEFFA5D7458844AB4EAFD7C154BA\" /><input type=\"hidden\" name=\"summary\" value=\"payment\" /><input type=\"hidden\" name=\"tradeDate\" value=\"20180302\" /><input type= name=\"tradeNo\" value=\"PfQW180302200400018366QP\" /><input type=\"hidden\" name=\"typeId\" value=\"3\" /><input type=\"hidden\" name=\"version\" value=\"1.0.0.0\" /></form></body><script type=\"text/javascript\"> document.getElementById('formRedirectToPayment').submit();</script></html>"
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
self.view = view
let webView = WKWebView(frame: view.bounds, configuration: WKWebViewConfiguration())
webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
webView.loadHTMLString(htmlString, baseURL: nil)
view.addSubview(webView)
}
}
如果你将htmlString
更改为像<h1>Hello World</h1>
这样的东西,它可以正常工作。
你也无法像这样加载HTML来执行原生javascript;相反,你将不得不添加一个WKUserScript
:
let js = "document.getElementById('formRedirectToPayment').submit()"
let config = WKWebViewConfiguration()
let userScript = WKUserScript(source: js, injectionTime: .atDocumentEnd, forMainFrameOnly: false)
config.userContentController.addUserScript(userScript)
let webView = WKWebView(frame: view.bounds, configuration: config)