Skip to content

Instantly share code, notes, and snippets.

@igaiga
Created October 24, 2011 10:19
Show Gist options
  • Save igaiga/1308730 to your computer and use it in GitHub Desktop.
Save igaiga/1308730 to your computer and use it in GitHub Desktop.
Set UIWebView's user agent into NSMutableURLRequest
NSString *urlString = [NSString stringWithFormat:@"http://example.com/foo"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
// get User Agent in UIWebView
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString *userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSLog(@"UserAgent: %@", userAgent);
[webView release];
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
NSURLResponse *response;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
if (error || [res statusCode] != 200) {
return nil;
} else {
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment