1、全局设置webview,不论是UIWebView还是WKWebView
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
NSString *ua = [NSString stringWithFormat:@"/xxxxflag/%@",app_Version];
UIWebView *webView = [[UIWebView alloc] init];
//直接获取userAgent
NSString *userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSString *newUserAgent = [userAgent stringByAppendingString:ua];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
}
2、全局设置webview,不论是UIWebView还是WKWebView
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.webView = [[WKWebView alloc] init];
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
NSString *ua = [NSString stringWithFormat:@"/xxxxflag/%@",app_Version];
if (@available(iOS 12.0, *)){
//由于iOS12的UA改为异步,所以不管在js还是客户端第一次加载都获取不到,所以此时需要先设置好再去获取
NSString *userAgent = [_webView valueForKey:@"applicationNameForUserAgent"];
NSString *newUserAgent = [NSString stringWithFormat:@"%@%@",userAgent,ua];
[_webView setValue:newUserAgent forKey:@"applicationNameForUserAgent"];
// NSLog(@"%@>>>%@>>>>>",userAgent,newUserAgent);
}
//在回调里获取userAgent即result
[_webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
NSString *userAgent = result;
if ([userAgent rangeOfString:ua].location != NSNotFound) {
return ;
}
NSString *newUserAgent = [userAgent stringByAppendingString:ua];
// NSLog(@"%@>>>%@>>>>>",userAgent,newUserAgent);
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent,@"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
[[NSUserDefaults standardUserDefaults] synchronize];
//不添加以下代码则只是在本地更改UA,网页并未同步更改
if (@available(iOS 9.0, *)) {
[_webView setCustomUserAgent:newUserAgent];
} else {
[_webView setValue:newUserAgent forKey:@"applicationNameForUserAgent"];
}
}]; //加载请求必须同步在设置UA的后面
}
注意:WKWebView不能使用局部声明,需要放在类扩展中,否则无效
3、页面内设置WKWebView,创建WKWebView之后调用以下方法
- (void)setWebViewUA {
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
NSString *ua = [NSString stringWithFormat:@"/xxxxflag/%@",app_Version];
if (@available(iOS 12.0, *)){
//由于iOS12的UA改为异步,所以不管在js还是客户端第一次加载都获取不到,所以此时需要先设置好再去获取
NSString *userAgent = [_webView valueForKey:@"applicationNameForUserAgent"];
NSString *newUserAgent = [NSString stringWithFormat:@"%@%@",userAgent,ua];
[_webView setValue:newUserAgent forKey:@"applicationNameForUserAgent"];
// NSLog(@"%@>>>%@>>>>>",userAgent,newUserAgent);
}
[_webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
NSString *userAgent = result;
if ([userAgent rangeOfString:ua].location != NSNotFound) {
return ;
}
NSString *newUserAgent = [userAgent stringByAppendingString:ua];
// NSLog(@"%@>>>%@>>>>>",userAgent,newUserAgent);
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent,@"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
[[NSUserDefaults standardUserDefaults] synchronize];
//不添加以下代码则只是在本地更改UA,网页并未同步更改
if (@available(iOS 9.0, *)) {
[_webView setCustomUserAgent:newUserAgent];
} else {
[_webView setValue:newUserAgent forKey:@"applicationNameForUserAgent"];
}
}]; //加载请求必须同步在设置UA的后面
}
4、页面内设置UIWebView,创建UIWebView之后调用以下方法
UIWebView *webView = [[UIWebView alloc] init];
NSString *userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSString *newUserAgent = [userAgent stringByAppendingString:@" Appended Custom User Agent"];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
注意
:UserAgent的设置都是要在webview初始化后,才能生效。
ViewController里面进行的局部设置,在页面dealloc后,UserAgent随之失效。
5、在项目的网络请求中,我们也可以进行httpHeader设置UserAgent
// AFN中 在 AFHTTPRequestSerializer 类中
//调用- (void)setValue:(nullable NSString *)value forHTTPHeaderField:(NSString*)field; 给请求头设置参数 @"User-Agent" 的值
网友评论