User-Agent not changing in NSMutableURLRequest
Asked Answered
S

2

2

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.

Selfassertion answered 16/12, 2013 at 18:19 Comment(3)
What happens if you try @"User-Agent" instead of @"User_Agent"?Picaroon
exactly you mentioned it above change it...Disraeli
Nothing happens... i tried both before this question and i left the wrong one... sorry about that. The question is already edited!. Besides that... it seems like the UIWebView is overwriting the User-Agent somehow, because if i send any other header parameter everything seems to work fine.Selfassertion
S
5

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.

Selfassertion answered 17/12, 2013 at 16:43 Comment(7)
Cool! That seems to work, but it's not documented anywhere, so it might be a bit dangerous to use in production code...Convey
I know! but i´ve been reading and it seems that as long as you don't use the swizzling method in the NSMutableURLRequest class, everything should be more than fine... i guess we'll see what happens! :). If something goes wrong with this approach i will let you know. Thank you Ivan.Selfassertion
@Selfassertion has your app hit the store already? I'm in the same situation as you and need to decide whether to use your workaround or not. So I'd like to know if this will pass through review :) Thanks alot!!Briton
Hi @Briton , no... i haven´t send the app to review yet, but i will soon! i´ll let you know what happens with it :)Selfassertion
Hello again @Toastor, i´ve sent the app to review and there´s no problems whatsoever with this workaround! so... feel free to use it!Selfassertion
Cool!! And thanks alot for getting back to me! :) I'd +1 your answer, but I already did - so I picked another instead. Or maybe two. :pBriton
Facing the very same problem - This solution didn't seem to work for me! How do you think this is going to work? just curious to know!!Adventurer
C
2

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.

Convey answered 17/12, 2013 at 10:57 Comment(2)
Hi Ivan, first of all, thank you for your answer... but, wouldn't i be losing the navigation history using this method?? If so, this' not the solution to my problem because sometimes i need to go back to the previous web page loaded into the UIWebView.Selfassertion
Hi, you'll need to manage that yourself as well, but it will be very simple. Just make a property (NSArray or NSMutableArray) in your controller and hold the NSURLRequests there. After that all you need to do is cancel the request in webView:shouldStartLoadWithRequest:navigationType: and load your NSURLRequest using NSURLConnection.Convey

© 2022 - 2024 — McMap. All rights reserved.