- (void)setUserAgent {
//获取webView的UserAgent
NSString *oldAgent = [self.shareWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
//获取UserAgent字符串的长度
NSInteger lent = oldAgent.length;
NSString *UA = [oldAgent substringFromIndex:lent-7];
//判断UserAgent的后缀是否已经添加,判断并作相应地处理
if (![UA isEqualToString:@"app-ios"]) {
//add my info to the new agent
NSString *newAgent = [oldAgent stringByAppendingString:@" app-ios"];
NSLog(@"new agent :%@", newAgent);
//重新设置UserAgent
NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
//记得重新loadView使UserAgent生效
[super loadView];
}
}
webView添加UserAgent后缀
最新推荐文章于 2021-06-01 05:06:32 发布