WKWebView的userAgent在iOS13上和Mac上safari的userAgent一样的问题

本文介绍如何在iOS13及iPadOS上通过设置WKWebView的WKWebpagePreferences来修正UserAgent,使其显示为移动设备而非桌面浏览器,解决因默认请求桌面网站设置导致的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iOS13

PadOS上,WKWebView的UserAgent变成了类似这样:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko)

其主要原因是iPadOS在设置-Safari浏览器-请求桌面网站的设置默认是开启的,只要把它关掉就正常了,但是不能要求所有用你的软件的人都去做这个设置。

WKWebView中新增的api中有个新的api叫做WKWebpagePreferences:

/*! @abstract The set of default webpage preferences to use when loading and rendering content.

@discussion These default webpage preferences are additionally passed to the navigation delegate

in -webView:decidePolicyForNavigationAction:preferences:decisionHandler:.

*/

@property (null_resettable, nonatomic, copy) WKWebpagePreferences *defaultWebpagePreferences API_AVAILABLE(macos(10.15), ios(13.0));
/*! @enum WKContentMode

@abstract A content mode represents the type of content to load, as well as

additional layout and rendering adaptations that are applied as a result of

loading the content

@constant WKContentModeRecommended  The recommended content mode for the current platform

@constant WKContentModeMobile      Represents content targeting mobile browsers

@constant WKContentModeDesktop      Represents content targeting desktop browsers

@discussion WKContentModeRecommended behaves like WKContentModeMobile on iPhone and iPad mini

and WKContentModeDesktop on other iPad models as well as Mac.

*/

typedef NS_ENUM(NSInteger, WKContentMode) {

    WKContentModeRecommended,

    WKContentModeMobile,

    WKContentModeDesktop

} API_AVAILABLE(ios(13.0));

/*! A WKWebpagePreferences object is a collection of properties that

determine the preferences to use when loading and rendering a page.

@discussion Contains properties used to determine webpage preferences.

*/

WK_EXTERN API_AVAILABLE(macos(10.15), ios(13.0))

@interface WKWebpagePreferences : NSObject

/*! @abstract A WKContentMode indicating the content mode to prefer

when loading and rendering a webpage.

@discussion The default value is WKContentModeRecommended. The stated

preference is ignored on subframe navigation

*/

@property (nonatomic) WKContentMode preferredContentMode API_AVAILABLE(ios(13.0));

@end

 WKContentModeRecommended behaves like WKContentModeMobile on iPhone and iPad mini
and WKContentModeDesktop on other iPad models as well as Mac.

我们只需要在初始化的时候按下面的方式设置,就可以设置WKWebpagePreferences的preferredContentMode为WKContentModeMobile,这样iPadOS上的UserAgent就正常了:

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
if (@available(iOS 13.0, *)) {
   configuration.defaultWebpagePreferences.preferredContentMode = WKContentModeMobile;

 此外还有和WKWebpagePreferences相关的新api,我们可以按需决定是否实现:

/*! @abstract Decides whether to allow or cancel a navigation.

@param webView The web view invoking the delegate method.

@param navigationAction Descriptive information about the action

triggering the navigation request.

@param preferences The default set of webpage preferences. This may be

changed by setting defaultWebpagePreferences on WKWebViewConfiguration.

@param decisionHandler The policy decision handler to call to allow or cancel

the navigation. The arguments are one of the constants of the enumerated type

WKNavigationActionPolicy, as well as an instance of WKWebpagePreferences.

@discussion If you implement this method,

-webView:decidePolicyForNavigationAction:decisionHandler: will not be called.

*/

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction preferences:(WKWebpagePreferences *)preferences decisionHandler:(void (^)(WKNavigationActionPolicy, WKWebpagePreferences *))decisionHandler API_AVAILABLE(macos(10.15), ios(13.0));

 

参考链接:https://www.jianshu.com/p/ae4755a2069b

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值