關於修改UIWebView的UserAgent

最近在作項目的時候須要修改webView的UserAgent,不然webView不能請求到數據,上網查了一下,其實挺多的,最重要的就是註冊默認的UserAgent, 利用registerDefaults便可,下面是源碼:web

 1 + (void)initialize {
 2     [super initialize];
 3     UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
 4     NSString *oldAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
 5     NSString *myAgent = @" xyy";
 6     if (![oldAgent hasSuffix:myAgent]) {
 7         NSString *newAgent = [oldAgent stringByAppendingString:myAgent];
 8         QLLog(@"new agent :%@", newAgent);
 9         NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil];
10         [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
11     }
12 }

須要移除的時候只要把最後的本身的agent去掉,而後再註冊便可lua

相關文章
相關標籤/搜索