浅谈iOS中的userAgent

作者: starfox寒流 | 来源:发表于2016-04-11 15:38 被阅读4921次

    User-Agent(用户代理)字符串是Web浏览器用于声明自身型号版本并随HTTP请求发送给Web服务器的字符串,在Web服务器上可以获取到该字符串。

    在公司产品中,在userAgent中增加了NetType字段,用于标识客户端当前的网络环境。

    • NetType/WIFI
    • NetType/3G

    web服务器端根据NetType类型加载不同分辨率的商品图片。我们只有两种,WIFI下高清图,非WIFI低分辨率图。

    <h3>获取WebView的userAgent</h3>

    iOS客户端中获取webView的userAgent的方法:

    NSMutableString *userAgent = [NSMutableString stringWithString:[[UIWebView new] stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]];
    

    根据需要添加不同的标识到新的userAgent中,这里添加一个WMall标识。

    NSString *newUagent = [NSString stringWithFormat:@"%@ WMall/%@", userAgent, [SystemInfo appShortVersion]];
    NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newUagent, @"UserAgent", nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
    

    newUagent 为Mozilla/5.0 (iPhone; CPU iPhone OS 9_3 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13E230 WMall/3.0

    <h3>根据网络类型设置不同的NetType标识</h3>

    iOS中判断网络状态一般用AFNetWorking中的方法:
    在appDelegate的application didFinishLaunchingWithOptions方法中启动网络状态监听:

    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    

    然后在基类BaseViewController中监听网络更新通知

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkReachabilityStatusDidChange:) name:AFNetworkingReachabilityDidChangeNotification object:nil];
    
    - (void)networkReachabilityStatusDidChange:(NSNotification *)notify{
        NSLog(@"网络变化通知:%@",notify.userInfo[AFNetworkingReachabilityNotificationStatusItem]);
        AFNetworkReachabilityStatus status = [notify.userInfo[AFNetworkingReachabilityNotificationStatusItem] integerValue];
        NSString *net = nil;
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:
                net = @"无网络";
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                net = @"WIFI";
                [self changeUserAgentWithType:@"WIFI"];
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                net = @"2G/3G/4G";
                [self changeUserAgentWithType:@"3G"];
                break;
            default:
                net = @"xxx";
                break;
        }
        [Error showError:[NSString stringWithFormat:@"当前网络状态%@",net]];
    }
    

    注意先判断userAgent中是否存在NetType

    //更新NetType
    - (void)changeUserAgentWithType:(NSString *)type{
        NSMutableString *userAgent = [NSMutableString stringWithString:[[UIWebView new] stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]];
        NSString *newUagent = nil;
        NSString *temp = nil;
        if ([userAgent containsString:@"NetType"]) {
            if ([userAgent containsString:@"WIFI"]) {
                temp = @"WIFI";
            }else if([userAgent containsString:@"3G"]){
                temp = @"3G";
            }
            newUagent = [userAgent stringByReplacingOccurrencesOfString:temp withString:type];
    
        }else{
            newUagent = [NSString stringWithFormat:@"%@ NetType/%@", userAgent, type];
        }
    
        NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newUagent, @"UserAgent", nil];
    
        NSLog(@"new useragent:%@",newUagent);
        [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
    }
    

    newUagent 为Mozilla/5.0 (iPhone; CPU iPhone OS 9_3 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13E230 WMall/3.0 NetType/WIFI

    参考
    微信6.0内嵌浏览器User-Agent字符串增加了NetType字段
    获取 UIWebview 的 Useragent,以及附加自定义字段到 Useragent

    相关文章

      网友评论

        本文标题:浅谈iOS中的userAgent

        本文链接:https://www.haomeiwen.com/subject/nrsilttx.html