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