Nov
19

Easily set the User-Agent in a UIWebView

Posted by mphweb on Fri, 11/19/2010 - 17:17, with 10 comments

UIWebView provides no way to change the User-Agent.

And not only it doesn't provide anything to change it, but it also override this value when we try to set the User-Agent in a NSMutableURLRequest object passed as a request in the loadRequest UIWebView method.

There are some solutions i found on the web that works but none are perfect :

One solution is to set User_Agent instead User-Agent in a NSMutableURLRequest.

That works for many websites but not all because setting User_Agent doesn't override User-Agent, that just adds another header value called User_Agent that some website uses instead of User-Agent.

Another solution that works well is to use method swizzling in the NSMutableURLRequest class to watch when the User-Agent is set and override the value to have the desired User-Agent.

The problem with this method is that is not very easy to implement and method swizzling can be dangerous if we are not careful with this.

The other problem is that Apple can reject your application if it uses method swizzling.

So i found (with luck) a solution which is simple, without using method swizzling and which overrides the true User-Agent (not User_Agent).

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

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

10 comments on "Easily set the User-Agent in a UIWebView"
Casey
Casey (not verified) - Dynamic UserAgent
Tue, 12/28/2010 - 11:14

Thanks for this awesome post. This is seemingly the only method that currently works without using undocumented methods.

I have this code working, however when I call the function again, to change the UserAgent to something else, UIWebView's do not update. In order to successfully update the UserAgent I need to remove/release the UIWebView and alloc/show it again.

-(void)setUserAgent:(NSString*)userAgent {
NSDictionary *userAgentReplacement = [[NSDictionary alloc] initWithObjectsAndKeys:userAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:userAgentReplacement];

//SHOULDN'T HAVE TO DO THIS
//get webView values then release
CGRect webFrame = webView.frame;
NSURLRequest *webRequest = [[webView request] retain];
[webView removeFromSuperview];
[webView release];

//allow webview and add to screen again
webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setScalesPageToFit:TRUE];
[webView setDelegate:self];
[self.view addSubview:webView];
}

This is mostly OK however it causes the UIWebView to lose all history. Is there any way to update the UserAgent without having to create a new UIWebView in order to have it take effect?

Thanks again

Anonyme - Interesting information about activation
Tue, 04/19/2011 - 01:21

I think this board is the proper place to ask you about the activation proccess. My link is not working properly, do you know why it is happening? http://www.mphweb.com/?15667bdbef97fc92aa0b25ff816,

Kieran
Kieran (not verified) - Fantastic. This works a treat
Wed, 12/29/2010 - 17:18

Fantastic. This works a treat and only 5 lines of code. I was stuck on this problem for ages. Thanks a lot

spstanley
spstanley (not verified) - Awesome solution, thank you
Wed, 01/05/2011 - 00:37

Awesome solution, thank you very much!

Eimantas
Eimantas (not verified) - Tip
Wed, 01/26/2011 - 09:12

You MUST use registerDefaults as setValue:forKey will not work!

inator
inator (not verified) - use with phonegap childbrowser
Sun, 01/30/2011 - 16:15

I'd like to apply this fix to the childbrowser plugin in phonegap. Anyone know how I'd do this? Having no real useful experience with obj-c, I do't know where to begin.

Adam Bossy
Adam Bossy (not verified) - Can you give us a sample of
Thu, 03/03/2011 - 05:14

Can you give us a sample of what belongs in "Desired User Agent?" I'm using:

Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8C134 Safari/528.16

... to no avail.

Also, I assume "UserAgent" is meant to be un-hypenated and is not a typo?

mphweb
mphweb - You can put what you want in
Sat, 04/02/2011 - 17:57

You can put what you want in the desired user agent.

Yes it's "UserAgent", there is no typo, but don't forget that you need to set the value very early in the app (it's why i put it in + (void)initialize)

optedoblivion
optedoblivion (not verified) - User Agent
Thu, 06/16/2011 - 17:28

I was struggling to find a good way to do this for android as well. This is what I've found

Really simple way is to fetch the android version from github. browse to framework/src/com/phonegap and edit DroidGap.java

scroll down to find the line

settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);

Then after that line add this

settings.setUserAgentString("HerpDerp Agent");

Jaye Jung
Jaye Jung (not verified) - great! it works perfectly!
Tue, 08/02/2011 - 08:33

great!

it works perfectly!

Post new comment
The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.