1. By changing URLRequest’s HTTP headers:
NSURL *url = [NSURL URLWithString: @"http://www.amazon.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:@kUserAgent forHTTPHeaderField:@"User-Agent"];
[webView loadRequest:request];

Actually, this won’t give the desired results as accessing that value in UIWebViewDelegate’s -webView:shouldStartLoadWithRequest: navigationType: will show. Altering passed the request passed as the argument (which is possible because that passed request is actually has type NSMutableURLRequest) won’t work either.

2. By altering user defaults (working solution):
#define kUserAgentKey @”UserAgent”

- (void)startSpoofingUserAgent:(NSString *)userAgent
 {
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{ kUserAgentKey: userAgent }];
}

- (void)stopSpoofingUserAgent 
{
    [[NSUserDefaults standardUserDefaults] unregisterDefaultForKey:kUserAgentKey];
}

Discussion: http://stackoverflow.com/questions/478387/change-user-agent-in-uiwebview-iphone-sdk

Advertisements