UIWebView&WKWebView的UA設(shè)置

UIWebView的UA設(shè)置

在使用UIWebView時(shí),更改user-agent,只能在原來(lái)系統(tǒng)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設(shè)置

也是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);
        }];
    }];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 用來(lái)修改的UA的webView 不要用來(lái)加載HTML ,不然起不到 UA更改的作用 參考鏈接 : http://w...
    MCWorld閱讀 495評(píng)論 0 0
  • iOS8之后,蘋果推出了WebKit這個(gè)框架,用來(lái)替換原有的UIWebView,新的控件優(yōu)點(diǎn)多多,不一一敘述。由于...
    TIME_for閱讀 18,306評(píng)論 63 142
  • 前言 關(guān)于UIWebView的介紹,相信看過(guò)上文的小伙伴們,已經(jīng)大概清楚了吧,如果有問題,歡迎提問。 本文是本系列...
    CoderLF閱讀 9,016評(píng)論 2 12
  • 通過(guò)學(xué)習(xí),你將會(huì)學(xué)習(xí)以下幾個(gè)方面的內(nèi)容: **什么是WKWebView以及它和UIWebView的區(qū)別是什么 **...
    SOI閱讀 11,772評(píng)論 18 42
  • 清晨隨筆: 《我的四月天》 我在春光中漫步走來(lái), 向低飛的燕子問聲好, 向戲水的雛鴨打個(gè)招呼, 喂,溫暖了,你們可...
    飄逸1閱讀 627評(píng)論 2 9