User-Agent(用戶代理)字符串是Web瀏覽器用于聲明自身型號版本并隨HTTP請求發(fā)送給Web服務(wù)器的字符串则奥,在Web服務(wù)器上可以獲取到該字符串。
在公司產(chǎn)品中狭园,在userAgent中增加了NetType字段读处,用于標(biāo)識客戶端當(dāng)前的網(wǎng)絡(luò)環(huán)境。
- NetType/WIFI
- NetType/3G
web服務(wù)器端根據(jù)NetType類型加載不同分辨率的商品圖片唱矛。我們只有兩種罚舱,WIFI下高清圖,非WIFI低分辨率圖绎谦。
<h3>獲取WebView的userAgent</h3>
iOS客戶端中獲取webView的userAgent的方法:
NSMutableString *userAgent = [NSMutableString stringWithString:[[UIWebView new] stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]];
根據(jù)需要添加不同的標(biāo)識到新的userAgent中,這里添加一個WMall標(biāo)識管闷。
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>根據(jù)網(wǎng)絡(luò)類型設(shè)置不同的NetType標(biāo)識</h3>
iOS中判斷網(wǎng)絡(luò)狀態(tài)一般用AFNetWorking中的方法:
在appDelegate的application didFinishLaunchingWithOptions
方法中啟動網(wǎng)絡(luò)狀態(tài)監(jiān)聽:
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
然后在基類BaseViewController中監(jiān)聽網(wǎng)絡(luò)更新通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkReachabilityStatusDidChange:) name:AFNetworkingReachabilityDidChangeNotification object:nil];
- (void)networkReachabilityStatusDidChange:(NSNotification *)notify{
NSLog(@"網(wǎng)絡(luò)變化通知:%@",notify.userInfo[AFNetworkingReachabilityNotificationStatusItem]);
AFNetworkReachabilityStatus status = [notify.userInfo[AFNetworkingReachabilityNotificationStatusItem] integerValue];
NSString *net = nil;
switch (status) {
case AFNetworkReachabilityStatusNotReachable:
net = @"無網(wǎng)絡(luò)";
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:@"當(dāng)前網(wǎng)絡(luò)狀態(tài)%@",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內(nèi)嵌瀏覽器User-Agent字符串增加了NetType字段
獲取 UIWebview 的 Useragent,以及附加自定義字段到 Useragent