//重写UserAgent
- (void)setUserAgent{
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString *oldAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
if ([oldAgent rangeOfString:@"新userAgent的标志"].location == NSNotFound) {
NSString *newAgent = [NSString stringWithFormat:@"新userAgent"];
NSDictionary *dictionnary = [NSDictionary dictionaryWithObjectsAndKeys:newAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
在- (void)viewWillAppear:(BOOL)animated中调用。
新初始化一个UIWebView重写UserAgent,就能规避在第二次进入才能修改的问题。
新userAgent仿微信的写法是:
if ([oldAgent rangeOfString:@"NetType"].location == NSNotFound) {
NSString *appInfoStr = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey] , [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey]];
NSString *netTypeStr = [NSString stringWithFormat:@"NetType/%@",HttpClient.netWork];
NSString *LanguageStr = [NSString stringWithFormat:@"Language/%@", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]];
NSString *newAgent = [NSString stringWithFormat:@"%@ %@ %@ %@", oldAgent,appInfoStr,netTypeStr,LanguageStr];
NSDictionary *dictionnary = [NSDictionary dictionaryWithObjectsAndKeys:newAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
[[NSUserDefaults standardUserDefaults] synchronize];
}
其中netType的写法是:
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
- (NSString *)netWork{
NSString *netWork = @" ";
NetworkReachabilityStatus status = self.networkReachablilityStatus;
if (status == NetworkReachabilityStatusReachableViaWiFi)
{
netWork = @"wifi";
}
else if(status == NetworkReachabilityStatusReachableViaWWAN)
{
CTTelephonyNetworkInfo * info = [[CTTelephonyNetworkInfo alloc] init];
NSString *currentRadioAccessTechnology = info.currentRadioAccessTechnology;
if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE])
{
netWork = @"4g";
}
else if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge] ||
[currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS] ||
[currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x])
{
netWork = @"2g";
}
else if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA] ||
[currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA] ||
[currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA] ||
[currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0] ||
[currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA] ||
[currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB] ||
[currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD])
{
netWork = @"3g";
}
else
{
netWork = @"Unknow";
}
}
else
{
netWork = @"Unknow";
}
return netWork;
}