UIWebView&WKWebView的UA设置

作者: brownfeng | 来源:发表于2016-06-21 15:11 被阅读3647次

    UIWebView的UA设置

    在使用UIWebView时,更改user-agent,只能在原来系统UA后面添加一些东西,stackoverflow上面有一种方法:

    http://stackoverflow.com/questions/478387/change-user-agent-in-uiwebview-iphone-sdk/23654363#23654363

     //get the original user-agent of webview
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
    NSString *oldAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    NSLog(@"old agent :%@", oldAgent);
    //add my info to the new agent
    NSString *newAgent = [oldAgent stringByAppendingString:@" Jiecao/2.4.7 ch_appstore"];
    NSLog(@"new agent :%@", newAgent);
    //regist the new agent
    NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
    

    WKWebView的UA设置

    也是stackover flow 上面的方法

    @property(nonatomic, strong) WKWebView *webView;
    
    
     [self.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            
            NSString *userAgent = result;
            NSString *newUserAgent = [userAgent stringByAppendingString:@" WeBank-Wepower/1.0.0"];
            
            NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];
            [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
            
            strongSelf.webView = [[WKWebView alloc] initWithFrame:strongSelf.view.bounds];
            strongSelf.webView.allowsBackForwardNavigationGestures = YES;
            strongSelf.webView.UIDelegate = self;
            strongSelf.webView.navigationDelegate = self;
            
            if (nil != self.urlString) {
                NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.urlString]];
                [request setTimeoutInterval:15];
                [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
                [strongSelf.webView loadRequest:request];
            }
            
            [strongSelf.view addSubview:self.webView];
    
            // After this point the web view will use a custom appended user agent
            [strongSelf.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
                NSLog(@"%@", result);
            }];
        }];
    

    相关文章

      网友评论

      • hdeyiji:大神,我想问问!我打印出来的是改了,但是抓包抓到的没有修改,怎么弄?
        hdeyiji:@brownfeng 不知道!一直设置不上!好郁闷!我可能要换uiwebview了
        brownfeng:后台看呢. 抓包设置问题吧. 我们这样生产用的很好
      • z时光旅人:按您说的设置后无效,iOS9上有customUserAgent的方法,iOS8上又该怎么设置呢?
        brownfeng:@z时光旅人 用第一个方法.先创建一个UIWebView,设置UA,然后删掉它,重新创建你需要的UIWebView或者WKWebView
        z时光旅人:@brownfeng 我现在在适配iOS8,但customUserAgent这个方法是在iOS9之后才有用的,iOS8上使用evaluateJavaScript修改UserAgent无效
        brownfeng:@z时光旅人 iOS 9以上有效的唉.我一直在用

      本文标题:UIWebView&WKWebView的UA设置

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