webview中的iOS 13 iPad用户代理问题
我有一段代码为我的所有web视图设置了用户代理:webview中的iOS 13 iPad用户代理问题,ios,ipad,wkwebview,user-agent,ios13,Ios,Ipad,Wkwebview,User Agent,Ios13,我有一段代码为我的所有web视图设置了用户代理: dispatch_once(&onceToken, ^{ { __block WKWebView *webView = [[WKWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds] configuration:[WKWebViewConfiguration new]]; [webView evaluat
dispatch_once(&onceToken, ^{
{
__block WKWebView *webView = [[WKWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds] configuration:[WKWebViewConfiguration new]];
[webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
if(nil != result && [result isKindOfClass:[NSString class]]) {
NSString *userAgent = result;
if( [userAgent rangeOfString:@"My Special User Agent"].length == 0 )
{
userAgent = [userAgent stringByAppendingString:@" My Special User Agent"];
NSDictionary * dictionary = @{@"UserAgent": userAgent};
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
}
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.125 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
webView = nil;
});
}];
}
});
然而,当我在iPad Pro(12.9英寸)-13.2.2上测试它时,我发现我得到的用户代理是错误的,并且在大多数情况下不包含我的特殊用户代理。有趣的是,如果我将web视图设置得足够小(大约占屏幕的20%),它确实可以工作
我找到了一种解决方法,可以在创建web视图时设置用户代理:
_webView.customUserAgent = [[NSUserDefaults standardUserDefaults] stringForKey:@"UserAgent"];
这是一个安全的解决方案吗(我可能会丢失用户代理中的一些重要信息)?
有更好的解决方案吗?苹果默认情况下从ios13的用户代理中删除了Ipad,因为
设置->Safari->请求桌面网站->所有网站。默认情况下已启用该选项。这可能与WKWebpagePreferences.ContentMode有关。@matt您知道是否需要设置
WKWebpagePreferences.ContentMode
?这会有什么影响呢?默认情况下,用户代理和内容模式紧密相连,内容模式取决于大小。