ER: Allow global HTTP header values for NSURL / CFNetwork

Originator:matt%bookhousesoftware.com
Number:rdar://9857248 Date Originated:7/28/2011
Status:Open Resolved:
Product:Mac OS X Product Version:N/A
Classification:Enhancement Reproducible:N/A
 
Summary:
It would be truly wonderful if we could set a global custom user-agent string that was used by NSURLConnection unless explicitly overridden in a custom NSURLRequest. Basically, this global value would be set to the request header whenever an NSURLRequest was created, allowing it to be one-off customized by the caller immediately afterwards as needed.

It might make sense to expose a global key-value request header store rather than solving the problem just for the user-agent. Setting to nil on a global or per-instance basis could restore the Apple default behavior, while setting to the empty string could deliberately submit a clear value.

Right now, developers must centralize all their NSURLRequest creation so that it contains a custom user-agent. Worse, though, is that there's little that can be done with third-party web service libraries in an app that can't be reached. In many cases, a solution like the one proposed here is the only option.

So the idea is:

[NSURLRequest setValue:@"MyApp v1.2 iOS" forHTTPHeaderField:@"User-Agent"]; // Class method; all NSURLRequests will now use this field value by default.

// ...

NSURLRequest *requestOne = [NSURLRequest requestWithURL:myURL]; // Preconfigured with the above UA

NSMutableURLRequest *customRequest = [NSMutableURLRequest requestWithURL:myURL]; // Also preconfigured

[customRequest setValue:@"AnotherUserAgent" forHTTPHeaderField:@"User-Agent"]; // Overrides global UA

[customRequest setValue:nil forHTTPHeaderField:@"User-Agent"]; // Falls back to the Apple default

Ideally, this would also affect requests by the Apple frameworks — AVPlayer and MPMoviePlayerController, for instance — but I'll log a separate bug for those just in case.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!