Reputation: 167
I am trying to get the user agent but when I try and read it, it comes out (null)
NSLog(@"user agent = %@", [request valueForHTTPHeaderField: @"User-Agent"]);
request is an NSURLRequest. So I tried to get the http headers and I don't think there are any. When I use
NSLog(@"http headers = %d", [[req allHTTPHeaderFields] fileSize]);
it prints out zero. req is an NSMutableURLRequest. Does anyone know why this is happening.
This is the method that I am using:
- (BOOL)webView:(UIWebView )webView2 shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSMutableURLRequest *req = (NSMutableURLRequest *)request;
NSString *versionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString)kCFBundleVersionKey];
NSLog(@"http headers = %@", [request allHTTPHeaderFields]);
NSLog(@"http headers = %d", [[req allHTTPHeaderFields] fileSize]);
[req setValue:[NSString stringWithFormat:@"myApp/%@ %@", versionString, [request valueForHTTPHeaderField:@"User-Agent"]] forHTTPHeaderField:@"User-Agent"];
NSLog(@"user agent = %@", [request valueForHTTPHeaderField: @"User-Agent"]);}
Upvotes: 1
Views: 8995
Reputation: 7337
This worked for me:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString* secretAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSLog(@"navigator.userAgent = %@", secretAgent);
NSDictionary* headers = [request allHTTPHeaderFields];
NSLog(@"headers: %@",headers);
NSString* ua = [request valueForHTTPHeaderField:@"User-Agent"];
NSLog(@"User-Agent = %@", ua);
}
I don't know why you're looking at filesize
when you can just look at the headers themselves.
Cf. https://stackoverflow.com/a/19184414/1431728
Upvotes: 2
Reputation: 19867
You don't have any headers in your request object because you haven't added any. If you want to specify the User-Agent
header, you need to add it yourself, as outlined here.
Upvotes: 0