Skip to content

Instantly share code, notes, and snippets.

@zafar007
Forked from igaiga/gist:1308730
Created September 9, 2016 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zafar007/2e64da9b5a69094b1971b9edc9ce00d6 to your computer and use it in GitHub Desktop.
Save zafar007/2e64da9b5a69094b1971b9edc9ce00d6 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