如何在swift中为iOS全局请求添加HTTP头 [英] How to add HTTP headers in request globally for iOS in swift

查看:1392
本文介绍了如何在swift中为iOS全局请求添加HTTP头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

请以任何方式帮助我。

please help me in any way.

推荐答案

AFAIK遗憾的是你不能用 WKWebView 来做到这一点。

AFAIK sadly you cannot do this with WKWebView.

它肯定不适用于 webView:decisionPolicyForNavigationAction: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.

这篇关于如何在swift中为iOS全局请求添加HTTP头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆