UIWebView iOS5 User Agent

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

What I have done so far: Using the delegate call, intercept NSURLRequest, create a new url request and set it as a user agent as I want, then load the data and reload the UIWebView using "loadData: MIMEType: ....".

Problem: This causes infinite recursion, where I load the data that the delegate calls, which the 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; } 
+35
ios objective-c cocoa-touch uiwebview
Dec 13 '11 at 10:26
source share
3 answers

Change the default value of "UserAgent" by executing this code once when your application 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 more information. To get the user agent, you can enable the Developer menu, install the user agent, and then connect to this site to print it for you: WhatsMyAgent . Similarly, you can connect using any mobile device and get it the same way. By the way, this still works great in iOS7 +

+88
Dec 29 '11 at 10:11
source share

In Swift, use this to install UserAgent ,

 func setUserAgent(){ var userAgent = NSDictionary(objectsAndKeys: "YourUserAgentName","UserAgent") NSUserDefaults.standardUserDefaults().registerDefaults(userAgent as [NSObject : AnyObject]) } 

Use this for testing,

 println(WebView.stringByEvaluatingJavaScriptFromString("navigator.userAgent")); 
+1
May 20 '15 at 8:11
source share

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

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

You must restrict your dispatch_async() block to be called, for example, using a regular 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; } 
0
Feb 27 '12 at 14:40
source share



All Articles