废话不多说,直接上代码,此方法需要在网页初始化加载链接之前就行调用。
-(void)setChangUserAgent:(NSString*)userAgent{
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:userAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults]
registerDefaults:dictionary];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.view insertSubview:self.webView atIndex:0];
if(@available(iOS11.0, *)) {
self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}else{
self.automaticallyAdjustsScrollViewInsets=NO;
// Fallback on earlier version
}
}
调用如下
[self setChangUserAgent:@"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36 iphoneapp"];