Skip to content

Instantly share code, notes, and snippets.

@Koze
Last active May 23, 2022 09:49
Show Gist options
  • Save Koze/cfda5d2af12f6215424e to your computer and use it in GitHub Desktop.
Save Koze/cfda5d2af12f6215424e to your computer and use it in GitHub Desktop.
Getting the user agent
// UIWebView
{
UIWebView *webView = [[UIWebView alloc] init];
[webView loadHTMLString:@"<html></html>" baseURL:nil];
NSString *appName = [webView stringByEvaluatingJavaScriptFromString:@"navigator.appName"];
NSLog(@"%@", appName);
// Netscape
NSString *userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSLog(@"%@", userAgent);
// iOS 8.3
// Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12F70
// iOS 9.0
// Mozilla/5.0 (iPhone; CPU iPhone OS 9_0 like Mac OS X) AppleWebKit/601.1.32 (KHTML, like Gecko) Mobile/13A4254v
}
// WKWebView
{
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
[webView loadHTMLString:@"<html></html>" baseURL:nil];
[webView evaluateJavaScript:@"navigator.appName" completionHandler:^(id __nullable appName, NSError * __nullable error) {
NSLog(@"%@", appName);
// Netscape
}];
[webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id __nullable userAgent, NSError * __nullable error) {
NSLog(@"%@", userAgent);
// iOS 8.3
// Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12F70
// iOS 9.0
// Mozilla/5.0 (iPhone; CPU iPhone OS 9_0 like Mac OS X) AppleWebKit/601.1.32 (KHTML, like Gecko) Mobile/13A4254v
}];
// needs retain because `evaluateJavaScript:` is asynchronous
self.webView = webView;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment