iOS 获取UserAgent

User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等。

获取UserAgent:在AppDelegate中添加如下代码,加上App独有属性。

    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
    NSString *oldAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"] ?:@"";
    //add my info to the new agent
    NSString *systemVersion  = [[UIDevice currentDevice] systemVersion];
    NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    CGFloat  iphoneScale     = [[UIScreen mainScreen] scale];
    NSString *model          = [[UIDevice currentDevice] model];
    NSString *localeIdentifier = [[NSLocale currentLocale] localeIdentifier];
    NSString *identifier = [[NSBundle mainBundle] bundleIdentifier];
    NSString *appendAgent = [NSString stringWithFormat:@"%@/%@ (%@/%@ ; %@; Scale/%0.2f)", identifier,currentVersion, model,systemVersion,localeIdentifier,iphoneScale];
    if ([oldAgent rangeOfString:appendAgent].location == NSNotFound) {
        NSString *newAgent  = [NSString stringWithFormat:@"%@ %@", oldAgent,appendAgent];
        XHLog(@"new agent :%@", newAgent);
        [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent": newAgent,@"HTTPUserAgent":appendAgent}];
    }

oldAgent:Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Mobile/15A372

WKWebView:获取方式为异步,一般放在控制器初始化WKWebView中,只有当在App中加载WKWebView网页的时候,才会回调到Block里面获取到userAgent的值。

// 方法是异步的
    WKWebViewConfiguration* webViewConfig = WKWebViewConfiguration.new;
    webViewConfig.allowsInlineMediaPlayback = YES;
    
    WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kStatusBarHeight - KNavgationBarHeight) configuration:webViewConfig];
    
    wkWebView.navigationDelegate = self;
    wkWebView.UIDelegate = self;
    [self.view addSubview:wkWebView];
[wkWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
    DLog(@"userAgent :%@", result);
    NSString *systemVersion  = [[UIDevice currentDevice] systemVersion];
    NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    CGFloat  iphoneScale     = [[UIScreen mainScreen] scale];
    NSString *model          = [[UIDevice currentDevice] model];
    NSString *localeIdentifier = [[NSLocale currentLocale] localeIdentifier];
    NSString *identifier = [[NSBundle mainBundle] bundleIdentifier];
    NSString *appendAgent = [NSString stringWithFormat:@"%@/%@ (%@/%@ ; %@; Scale/%0.2f)", identifier,currentVersion, model,systemVersion,localeIdentifier,iphoneScale];
    if ([result rangeOfString:appendAgent].location == NSNotFound) {
        NSString *newAgent  = [NSString stringWithFormat:@"%@ %@", result,appendAgent];
        XHLog(@"new agent :%@", newAgent);
        [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent": newAgent,@"HTTPUserAgent":appendAgent}];
 }];

result:Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Mobile/15A372

你可能感兴趣的:(iOS 获取UserAgent)