浅谈iOS中的userAgent

时间:2023-12-11 19:10:56

浅谈iOS中的userAgent

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

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

  • NetType/WIFI
  • NetType/3G

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

获取WebView的userAgent

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

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

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