Question

I know there may be several issues about this, but i´m facing a problem when i try to set a new "user agent" for my UIWebView. Here is what i am doing right now:

NSURL *myUrl = [NSURL URLWithString:auxUrl];
    NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:myUrl];

    NSMutableString *auxUserAgent = [[NSMutableString alloc] initWithString:[myRequest valueForHTTPHeaderField: @"User-Agent"]];
    [auxUserAgent appendString:@"(iOS)"];

    [myRequest setValue:auxUserAgent forHTTPHeaderField:@"User-Agent"];

    NSLog(@"UA : %@",[myRequest valueForHTTPHeaderField: @"User-Agent"]);

I am trying to append some text to the actual user agent, and in the XCode console it does show the new user agent value.

2013-12-16 19:03:42.200 App[736:60b] UA : Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/11B554a(iOS)

But when i run my App, it shows the old one. Here is a screenshot of the user agent shown in the web page i am showing in the UIWebView: ScreenShot

So.. my questions are: What am i missing?? is there something in the system that doesn´t allow developers to change this data??

Thank you.

Was it helpful?

Solution

I "solved" my problem by putting in the didFinishLaunchingWithOptions of my AppDelegate.m the following code:

 NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:@"YOUR CUSTOM USER-AGENT", @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];

The thing is that i cannot append the text to the user agent itself, but i can write the data i need to differentiate between the access to the web page from the different apps or web browsers.

Thank you all for your help.

OTHER TIPS

It won't work like that. UIWebView overrides the User-Agent field.

If you try to log the User-Agent in the webView:shouldStartLoadWithRequest:navigationType: method, you'll see that it's already overridden, however you can use NSURLConnection to load your request with a custom User-Agent and then use the loadHTMLString:baseURL: method or loadData:MIMEType:textEncodingName:baseURL: method from UIWebView to display the downloaded data from NSURLConnection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top