iOS

Set User Agent for UIWebView

There is a very easy (but not documented) way to set the User Agent header for HTTP requests sent via UIWebView.

I find saw the solution from mphweb.

Basically, you just set register it with NSUserDefaults.

1
2
3
4
5
6
+ (void)initialize {
    // Set user agent (the only problem is that we can't modify the User-Agent later in the program)
    NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:@"Your desired user agent", @"UserAgent", nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
    [dictionnary release];
}

Comments