What HTTP User-Agent does my iOS program advertise itself as?

别等时光非礼了梦想. 提交于 2019-12-05 00:34:29

Your original assumption is partly incorrect- A custom User-agent string is used for NSURLRequests from your app. In my testing, the string is

<product-name>/<build-number> CFNetwork/548.0.3 Darwin/11.2.0

However, some requests from UIWebView use this user-agent string

Mozilla/5.0 (iPhone Simulator; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9A334

presumably so that websites can optimise their HTML for the device even if it's not MobileSafari.

According to this blog post, you may be able to set a pseudo-global User-Agent string (pseudo in that I'm not sure which other classes outside of UIWebView use it).

Here is the class method to add to your main controller (or app delegate) :

+ (void)initialize {
    // Set user agent (the only problem is we can't modify it later)
    NSDictionary *dictionary =
    [[NSDictionary alloc] initWithObjectsAndKeys:
     @"Your desired user agent", @"UserAgent", nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
    [dictionary release];
}

According to the comments on that post, "we can't modify it later" is a bit of an overstatement: you can modify the UserAgent value later, but you have to release and re-alloc any UIWebViews (and I presume NSURLConnections if they use it too) for the changes to take effect.

Michael Dautermann

Looking at this related question ( Changing the userAgent of NSURLConnection ), it looks like it is pretty easy to make a user-agent change for NSURLConnection.

As for the other classes (UIWebView, AVPlayer, MPMoviePlayerViewController), there's no easy way to mess with the underlying NSURLConnections.

If you really want to change the user-agent for all HTTP requests, I'd suggest looking into Objective-C Class Posing to replace NSURLConnection (which hopefully wouldn't require completely reimplementing it).

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