In iPadOS, the UserAgent of WKWebView becomes something like this:

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

This leads to problems with the front end where UserAgent determines the device. The main reason is that iPadOS is turned on by default in the Settings -Safari – request desktop site. You just turn it off and it works, but you can’t ask the reviewer or the user to turn it off. Therefore, some other methods were temporarily adopted to adapt, which were also referred to as follows:

Forums.developer.apple.com/thread/1221…

Stackoverflow.com/questions/5…

And so on… The others really don’t remember the address.

WKWebView has a new API called 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), the ios (13.0));Copy the code
/ *! @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)); @endCopy the code

I looked it over and made sure it was what I was looking for.

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

The preferredContentMode for WKWebpagePreferences is set to WKContentModeMobile, and the UserAgent on the iPadOS will work:

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; If (@ the available (iOS 13.0, *)) {configuration. DefaultWebpagePreferences. PreferredContentMode = WKContentModeMobile; }Copy the code

There are also new apis associated with WKWebpagePreferences that we can implement as needed:

/ *! @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), the ios (13.0));Copy the code

After finishing the adaptation, I also checked the information on the Internet, and found that many articles did not go into the depth of the previous strange solution, or maybe the big guys are too lazy to summarize it, so I share my solution here (or take a good look at the API update).