UIWebView iOS5 changing user-agent
Asked Answered
W

3

37

How can I change the user-agent of UIWebView in iOS 5?

What I have done so far: Using the delegate call back, intercept the NSURLRequest, create a new url request and set it's user-agent as whatever I want, then download the data and reload the UIWebView with "loadData:MIMEType:....".

Problem: This causes infinite recursion, where I load the data, which calls the delegate back, which intern calls the delegate....

Here's the delegate method:

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



    dispatch_async(kBgQueue, ^{
        NSURLResponse *response = nil;
        NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[request URL]];
        NSDictionary *headers = [NSDictionary dictionaryWithObject:
                                 @"custom_test_agent" forKey:@"User-Agent"];
        [newRequest setAllHTTPHeaderFields:headers];
        [self setCurrentReqest:newRequest];
        NSData *data = [NSURLConnection sendSynchronousRequest:newRequest 
                                             returningResponse:&response 
                                                         error:nil];
        dispatch_sync(dispatch_get_main_queue(), ^{
            [webView loadData:data 
                     MIMEType:[response MIMEType] 
             textEncodingName:[response textEncodingName] 
                      baseURL:[request URL]];
        });
    });

    return YES;
}
Wickham answered 13/12, 2011 at 10:26 Comment(2)
Please share setCurrentReqest method.Cele
Possible duplicate of Change User Agent in UIWebViewAlbina
A
89

Change the "UserAgent" default value by running this code once when your app starts:

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Your user agent", @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];  

EDIT: I have used this with great success, but want to add additional details. To get a user agent, you can enable the "Developer" menu, set the user agent, and then connect to this site to get it printed out for you: WhatsMyAgent. Likewise you can connect using any kind of mobile device, and get it that way too. BTW this is still working just fine in iOS7+

Autobiographical answered 29/12, 2011 at 10:11 Comment(9)
That works correctly. The issue with the original post about setting it in shouldStartLoadWebRequest: is that method is only called on the top level request, so all the internal requests get the old user-agent. I verified this using a fiddler2 proxy. Setting it in standardUserDefaults causes all requests to get the new user-agent. Thanks for the above code!Saddlebacked
Finally ... something that Actually works.... THANK A LOT MARTIN.... i even found components on github that surprisingly were too complicated to work with... your solution was simple and easy to implement...Ultrasonic
Works on iOS 4.3.3 as well. Anyone who can test it on 4.0?Bedside
Works great! I had to set it in the +(void) initialize{} method in my AppDelegate.mm of my phonegap appMechanist
super! but is it possible to get current headers and modify it ?Infeasible
This worked perfectly: + (void)initialize { NSDictionary *dictionary = @{ @"UserAgent" : @"Safari iOS 5.1 - iPhone"}; [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; }Toenail
Will it append this new user agent string to the default user agent string? or will overwrite that?Anyplace
@DavidH, your version fixes part of my problem. Thanks for including the user-agent string! Where did you get it? I'm wondering if a different string would solve the rest of my problem.Televisor
@Adil look here for more info. What I used worked for me a while ago, some post on StackOverflow: enterpriseios.com/wiki/UserAgentToenail
T
1

In Swift use this to set UserAgent,

func setUserAgent(){

    var userAgent = NSDictionary(objectsAndKeys:  "YourUserAgentName","UserAgent")

    NSUserDefaults.standardUserDefaults().registerDefaults(userAgent as [NSObject : AnyObject])

}

Use this to test,

println(WebView.stringByEvaluatingJavaScriptFromString("navigator.userAgent"));
Tetrarch answered 20/5, 2015 at 8:11 Comment(0)
R
0

When you send message [aWebView loadData:MIMEType:textEncodingName:baseURL:]

then aWebView shouldStartLoadWithRequest: will be called again, and then again - that is why you get an infinite recursion

You should restrict calling of your dispatch_async() block, for example by using some conventional URL:

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


    if ([[[request URL] absoluteString] isEqualToString:@"http://yourdomain.com/?local=true"]) {
        return YES;
    }


    ...

    dispatch_async(...
            [aWebView loadData:data 
                      MIMEType:[response MIMEType] 
              textEncodingName:[response textEncodingName] 
                       baseURL:[NSURL URLWithString:@"http://yourdomain.com/?local=true"]];

    );
    return NO;
}
Receivable answered 27/2, 2012 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.