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; }
ios objective-c cocoa-touch uiwebview
0xSina Dec 13 '11 at 10:26 2011-12-13 10:26
source share