1、在viewDidLoad里面添加
- (void)viewDidLoad {
[super viewDidLoad];
// 添加UserAgent 信息,UIWebView必须是空的,如果是已经创建好了就用[UIWebView alloc]init]
NSString *userAgent = [[[UIWebView alloc]init] stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSLog(@"前:%@",userAgent);
NSString *newUagent = [NSString stringWithFormat:@"%@ IOS_HUI/1.0",userAgent];
NSDictionary *dictionary = @{@"UserAgent":newUagent};
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
}
2、在webViewDidFinishLoad里面打印添加后的结果
-(void)webViewDidFinishLoad:(UIWebView *)webView{
//打印修改后的userAgent
NSString *newUagent1=[webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSLog(@"后:%@",newUagent1);
}