ios 如何在 UIWebView 请求中添加自定义 HTTP 标头,我的 UIWebView 基于 Cordova 项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25539837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add customize HTTP headers in UIWebView request, my UIWebView is based on Cordova project?
提问by jianhua
My iOS UIWebView page is based on Cordova open source framework, and I want to add some customize http headers in its webview URL request, my solution is to add them in the following UIWebView delegate method.
我的iOS UIWebView 页面基于Cordova 开源框架,我想在其webview URL 请求中添加一些自定义http 标头,我的解决方案是将它们添加到以下UIWebView 委托方法中。
Debug shows that headers are added successfully, but in fact the request doesn't bring them out. Using Wireshark to capture network packets and found only standard headers are available, no my customize ones.
Debug 显示 headers 添加成功,但实际上请求并没有把它们带出来。使用 Wireshark 捕获网络数据包,发现只有标准标头可用,没有我自定义的标头。
My testing is based on simulator (iOS 7.1), anyone who has experience on this topic please share and discuss together, thanks in advance.
我的测试基于模拟器(iOS 7.1),任何有这方面经验的人请一起分享和讨论,在此先感谢。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// Add customize http headers in UIWebView request
if([request isKindOfClass:[NSMutableURLRequest class]]) {
NSMutableURLRequest * mRequest = (NSMutableURLRequest *)request;
[mRequest setValue:@"1.1" forHTTPHeaderField:@"appVersion"];
[mRequest setValue:@"iPhone 4S" forHTTPHeaderField:@"deviceModel"];
}
return [super webView:webView shouldStartLoadWithRequest:request navigationType:navigationType];
}
采纳答案by Shams Ahmed
You have two options either create a NSMutableUrlRequestat the start and load that with webView loadReqest or take over the complete URL loading of your app with NSURLProtocol.
你有两个选择要么创建一个NSMutableUrlRequest开始和负载与web视图loadReqest或接管您的应用程序的完整URL负载NSURLProtocol。
The most easiest way is the first choice as its only one extra lines of code:
最简单的方法是首选,因为它只有一行额外的代码:
[webView loadRequest:mRequest];
The second choice uses NSURLProtocolto take over URL loading of your app. this involves registering your own solution using creating a concrete class. the main method to override is canonicalRequestForRequest
.
第二种选择使用NSURLProtocol来接管应用程序的 URL 加载。这涉及使用创建具体类来注册您自己的解决方案。覆盖的主要方法是canonicalRequestForRequest
.
I suggest you take a look at these two tutorials NSNipsterand raywenderlichfor guides.
我建议您查看NSNipster和raywenderlich这两个教程以获得指南。
回答by Umair Khalid
I know its late but may help others for SWIFT 3.0
我知道为时已晚,但可能会帮助其他人使用 SWIFT 3.0
let weburl = NSURL(string: "http://www.mywebsite.com")
let request = NSMutableURLRequest(URL: weburl!)
request.setValue("HEADER_VALUE", forHTTPHeaderField:"HEADER_NAME")
myWebView.loadRequest(request)
回答by Satyam Raikar
Sometimes Cookies are not set even after you assign all http headers. it is better to create mutable request and copy your nsurlrequest and add your custom header to it so that all information from original request is retained in mutable one.
有时,即使您分配了所有 http 标头,也不会设置 Cookie。最好创建可变请求并复制您的 nsurlrequest 并将您的自定义标头添加到其中,以便原始请求中的所有信息都保留在可变请求中。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
if(check if key not present){
NSMutableURLRequest *re = [[NSMutableURLRequest alloc] init];//alloc init not required
re = (NSMutableURLRequest *) request.mutableCopy;
[re setValue:@"Your Custom Value" forHTTPHeaderField:@"Yout Custom Header"];
[webView loadRequest:re] ;
return NO;
}
return YES;
}
回答by valvoline
a pureSwift 4 compliance answer should be something like the following:
一个纯粹的雨燕4合规的答案应该是像下面这样:
if let anURL = URL(string: aString) {
var aRequest = URLRequest(url: anURL, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 5.0)
if let tmpToken = Network.shared.getAuthenticationToken() {
aRequest.setValue(tmpToken, forHTTPHeaderField: "Authorization")
}
self.loadRequest(aRequest)
}
where cachePolicy, timeoutIntervalare to be set following your needs, as well as the if let statement in order to get an hypothetical token to be inserted in the request.
其中cachePolicy、timeoutInterval将根据您的需要进行设置,以及 if let 语句以便获取要插入到请求中的假设令牌。
The relevant part here is the wayyou set additional parameters on the URLRequest. No needs to use a Mutablestatement anymore. varis the you go on the URLRequest.
这里的相关部分是您在 URLRequest 上设置附加参数的方式。不再需要使用Mutable语句。var是您在URLRequest 上进行的操作。