【问题标题】:iOS 13 iPad user agent problem in webviewWebview中的iOS 13 iPad用户代理问题
【发布时间】:2019-11-18 21:38:03
【问题描述】:

我有一段代码为我的所有网络视图设置了用户代理:

   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"];

这是一个安全的解决方案(我可能会丢失用户代理中的一些重要信息)吗? 有没有更好的解决方案?

【问题讨论】:

  • 这可能与 WKWebpagePreferences.ContentMode 有关。
  • @matt 不知道是否需要设置WKWebpagePreferences.ContentMode?这会有什么影响?
  • 嗯,默认情况下,用户代理和内容模式是密切相关的,内容模式取决于大小。

标签: ios ipad wkwebview user-agent ios13


【解决方案1】:

苹果默认从ios13的userAgent中移除了Ipad,因为

设置 -> Safari -> 请求桌面网站 -> 所有网站。该选项默认启用。

【讨论】:

    猜你喜欢
    • 2017-05-26
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 2020-09-23
    • 2020-01-20
    • 2019-12-12
    • 2012-07-09
    • 2014-02-02
    相关资源
    最近更新 更多