Clinton Walsh
Clinton Walsh

Reputation: 167

change user-agent for good

I have been able to change the user-agent by using

- (BOOL)webView:(UIWebView *)webView2 shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

NSMutableURLRequest *req = (NSMutableURLRequest *)request;
NSString *versionString = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString*)kCFBundleVersionKey];

NSLog(@"user agent = %@", [req valueForHTTPHeaderField: @"User-Agent"]);

if ([req valueForHTTPHeaderField:@"User-Agent"] != NULL) {

    [req setValue:[NSString stringWithFormat:@"%@ %@", versionString, [req valueForHTTPHeaderField:@"User-Agent"]] forHTTPHeaderField:@"User-Agent"];
    NSLog(@"user agent = %@", [req valueForHTTPHeaderField: @"User-Agent"]);

   return YES
    }
}

But when I go to this website on the device http://whatsmyuseragent.com/ my user-agent hasn't changed. Is there a way to change the user-agent for good?

Upvotes: 3

Views: 1275

Answers (4)

Mitch
Mitch

Reputation: 11

After playing around for a really long time, I discovered that you can change the User Agent without rooting. Not sure if this helps you if you are building an app.

Upvotes: 1

Stephen Darlington
Stephen Darlington

Reputation: 52565

You can change the value of the user agent when doing "manual" NSURLRequests but I don't think you can change them in a UIWebView.

Reason being, the values that are passed into the method you show above are not mutable -- you can't change them and expect that the rest of the application will "take" the modifications. Honestly, I'm surprised that casting a NSURLRequest to its mutable variant and then changing it didn't cause your app to crash.

Upvotes: 0

David Nelson
David Nelson

Reputation: 752

I have noticed that the simulator reports nil when testing at times. I'm not sure if this is intentional or on purpose.

Upvotes: 0

user149341
user149341

Reputation:

You cannot change the user-agent displayed by Safari, only the one used by your own application.

Upvotes: 0

Related Questions