我是ios,swift和Xcode的新手。
我有一个onClick
的组件。我想在WKWebView
时添加一些标题,以便重定向到Android
中的另一个页面。如何覆盖或添加标题?如果在webView.setWebViewClient(new WebViewClient() {
:
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest url) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
view.loadUrl(url.getUrl().toString(), extraHeader);
}
return true;
}
:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
view.loadUrl(url, extraHeader);
}
return true;
}
:
}
版本中,我可以这样做:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = URL(string: BASE_URL)
let request = NSMutableURLRequest(url: url!)
request.addValue(MOBILE, forHTTPHeaderField: X_CLIENT_TYPE)
self.webView.load(request as URLRequest)
}
我已经这样做了:
WKWebView
但是,上面的代码仅用于首次加载。如果我点击a = ["Isolated-1", "SVT_FedPortGroup", "SVT_StoragePortGroup", "VM Network", "test-pg-2002"]
b = ["target_Isolated-1", "target_SVT_FedPortGroup", "target_SVT_StoragePortGroup", "target_VM Network", "target_test-pg-2002"]
c = {
"NetworkMaps": []
}
for (var i = 0; i < a.length; i++) {
c.NetworkMaps.push({
"ENVID": null,
"SourcePG": a[i],
"TargetPG": b[i]
})
}
console.log(c);
中的链接,标题就会消失。
所以,我很高兴有人能帮助我......
ENV:
- XCode v.9.3
答案 0 :(得分:0)
var request = URLRequest(url: URL(string: url)!)
request.httpMethod = "POST"
// If you have post parameter pass here
let postString = "key=\(Value!)&Key=\(Value!)"
request.httpBody = postString.data(using: .utf8)
// Set your Header here
request.setValue("MOBILE", forHTTPHeaderField:"X_CLIENT_TYPE")
if request != nil
{
webView.loadRequest(request)
webView.delegate = self
}
你可以做二次点击
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType:
UIWebViewNavigationType) -> Bool
{
var Myrequest = URLRequest.init(url: request.url!)
Myrequest.setValue("MOBILE", forHTTPHeaderField:"X_CLIENT_TYPE")
webView.loadRequest(Myrequest)
true
}
答案 1 :(得分:0)
我已经找到了解决方案:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
print("decidePolicyFor")
let request = navigationAction.request
print("request: ", request)
if ( (request.url?.absoluteString.hasPrefix(BASE_URL))! && request is NSMutableURLRequest) {
print("NSMutableURLRequest")
let mutableRequest = request as! NSMutableURLRequest;
let clientType = mutableRequest.value(forHTTPHeaderField: X_CLIENT_TYPE) as String?
print("clientType: ", clientType)
if (clientType != nil && clientType == MOBILE) {
print("client type is MOBILE")
} else {
print("add client type MOBILE")
mutableRequest.addValue(MOBILE, forHTTPHeaderField: X_CLIENT_TYPE)
webView.load(mutableRequest as URLRequest)
}
decisionHandler(WKNavigationActionPolicy.allow)
} else {
print("NOT NSMutableURLRequest")
decisionHandler(WKNavigationActionPolicy.allow)
}
}