UIWebView的UA設(shè)置
在使用UIWebView時(shí)今野,更改user-agent杂瘸,只能在原來系統(tǒng)UA后面添加一些東西惑朦,stackoverflow上面有一種方法:
//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);
}];
}];