-
-
Save zafar007/2e64da9b5a69094b1971b9edc9ce00d6 to your computer and use it in GitHub Desktop.
Set UIWebView's user agent into NSMutableURLRequest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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