UIWebView的UA设置
在使用UIWebView时,更改user-agent,只能在原来系统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设置
也是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);
}];
}];
网友评论