(转)webView使用userAgent判断是否进行加载方式

2018-08-08  本文已影响0人  遠遊旳遊子

文章来源:李洪强博客 - 浅谈iOS中的userAgent

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

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

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

1、获取WebView的userAgent

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

UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString * userAgent = [webView 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

2、根据网络类型设置不同的NetType标识

iOS中判断网络状态一般用AFNetWorking中的方法:
appDelegateapplication 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
上一篇下一篇

猜你喜欢

热点阅读