如何快速在 iOS 的全局请求中添加 HTTP 标头 [英] How to add HTTP headers in request globally for iOS in swift
问题描述
func webView(webView: WKWebView!, decidePolicyForNavigationAction navigationAction: WKNavigationAction!, decisionHandler: ((WKNavigationActionPolicy) -> Void)!) {
var request = NSMutableURLRequest(URL: navigationAction.request.URL)
request.setValue("value", forHTTPHeaderField: "key")
decisionHandler(.Allow)
}
在上面的代码中,我想向请求添加一个标头.我试过 navigationAction.request.setValue("IOS", forKey: "DEVICE_APP")
但它不起作用.
In the above code I want to add a header to the request.
I have tried to do navigationAction.request.setValue("IOS", forKey: "DEVICE_APP")
but it doesn't work.
请以任何方式帮助我.
推荐答案
AFAIK 遗憾的是你不能用 WKWebView
做到这一点.
AFAIK sadly you cannot do this with WKWebView
.
它肯定在 webView:decidePolicyForNavigationAction:decisionHandler:
中不起作用,因为 navigationAction.request
是只读的并且是非可变的 NSURLRequest
代码> 不能更改的实例.
It most certainly does not work in webView:decidePolicyForNavigationAction:decisionHandler:
because the navigationAction.request
is read-only and a non-mutable NSURLRequest
instance that you cannot change.
如果我理解正确,WKWebView
在单独的内容和网络进程中运行沙盒,至少在 iOS 上,没有办法拦截或更改它的网络请求.
If I understand correctly, WKWebView
runs sandboxed in a separate content and network process and, at least on iOS, there is no way to intercept or change it's network requests.
如果您退回到 UIWebView
,您可以执行此操作.
You can do this if you step back to UIWebView
.
这篇关于如何快速在 iOS 的全局请求中添加 HTTP 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!