Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the default user-agent string in an NSURLConnection?

I would like to append text to the default user-agent header in an NSURLConnection. I know how to change the user-agent of the NSURLConnection, but I don't see how to get the default user-agent. I tried the following:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
NSString *userAgent = [request valueForHTTPHeaderField:@"User-Agent"];
userAgent = [userAgent stringByAppendingString:extraUserAgentInfo];
[request addValue:userAgent forHTTPHeaderField:@"User-Agent"];

This does not work because userAgent is coming back nil from the valueForHTTPHeaderField: call.

like image 728
Brandon DuRette Avatar asked Apr 15 '11 15:04

Brandon DuRette


People also ask

Where is the User-Agent string?

The User-Agent (UA) string is contained in the HTTP headers and is intended to identify devices requesting online content. The User-Agent tells the server what the visiting device is (among many other things) and this information can be used to determine what content to return.

What is a User-Agent string?

A browser's User-Agent string (UA) helps identify which browser is being used, what version, and on which operating system. When feature detection APIs are not available, use the UA to customize behavior or content to specific browser versions.

What does a User-Agent string contain?

Described in the HTTP standard, the User-Agent string contains a number of tokens that refer to various aspects of the request, including the browser's name and version, rendering engine, device's model number, operating system and its version, etc.


1 Answers

Part of the default User-Agent is your AppName and Version:

My user agent is:

User-Agent  foo-bar/1.0 CFNetwork/609.1.4 Darwin/12.4.0

foo-bar is the value of [[NSBundle mainBundle] objectForInfoDictionaryKey:(__bridge NSString *) kCFBundleNameKey]

and

1.0 is the value of [[NSBundle mainBundle] objectForInfoDictionaryKey:(__bridge NSString *) kCFBundleVersionKey]

like image 79
Heath Borders Avatar answered Oct 28 '22 12:10

Heath Borders