Last active
May 23, 2022 09:49
-
-
Save Koze/cfda5d2af12f6215424e to your computer and use it in GitHub Desktop.
Getting the user agent
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
// 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 | |
} |
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
// 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