在WKWebView开发者中,有时候,在APP端嵌套h5页面的时候,为了区分去WAP端还是APP端,通常需要做区分标示,这样一般,是通过修改UserAgent来做区分标示,比如,我们项目中使用gomeshop来区分,
-(void)changeUserAgent{
if (@available(iOS 9.0, *)) {
//设置setCustomUserAgent 这个导致 没有浏览器内核信息
// [self.wkWebView setCustomUserAgent:@"gomeshop"];
// [self.wkWebView setValue:@"gomeshop" forKey:@"applicationNameForUserAgent"];
UIWebView * webView = [[UIWebView alloc]initWithFrame:CGRectZero];
NSString *userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
userAgent = [userAgent stringByAppendingString:@" GomeShop"];
[self.wkWebView setCustomUserAgent:userAgent];
}else{
UIWebView * webView = [[UIWebView alloc]initWithFrame:CGRectZero];
NSString *userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
userAgent = [userAgent stringByAppendingString:@" GomeShop"];
NSDictionary * customUserAgent = [NSDictionary dictionaryWithObjectsAndKeys:userAgent,@"UserAgent", nil];
[[NSUserDefaults standardUserDefaults]registerDefaults:customUserAgent];
[[NSUserDefaults standardUserDefaults]synchronize];
}
NSLog(@"configuration.applicationNameForUserAgent***%@", self.wkWebView.configuration.applicationNameForUserAgent);
[self.wkWebView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
NSLog(@"changeUserAgent******%@",result);
}];
}