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

五迷三道 提交于 2019-12-06 19:10:40

问题


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.


回答1:


There's a blog entry Changing the headers for UIWebKit HTTP requests from iCab. He suggest using a process call "Method Swizzling” to get what you want. But there is a rumor that Method swizzling cause for 4.0 rejection




回答2:


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]



来源:https://stackoverflow.com/questions/5679305/how-do-i-get-the-default-user-agent-string-in-an-nsurlconnection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!