美文网首页
获取 UIWebview 的useragent

获取 UIWebview 的useragent

作者: 来宝 | 来源:发表于2016-05-07 12:44 被阅读273次

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);
}

相关文章

网友评论

      本文标题:获取 UIWebview 的useragent

      本文链接:https://www.haomeiwen.com/subject/xfzjrttx.html