iOS13 iPadOS WKWebView獲取到的UserAgent中的設備變成Macintosh的問題

前陣子適配 iOS13 的時候遇到一個關于WKWebView設置UserAgent的問題,在iPadOS上,WKWebView的UserAgent變成了類似這樣:

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

這就導致前端根據UserAgent判斷設備的地方出了問題,當時發現其主要原因是iPadOS在設置-Safari瀏覽器-請求桌面網站的設置默認是開啟的,只要把它關掉就正常了,但是你又不能要求審核人員或者用戶自己去把它關了,所以當時臨時采取了一些其他方法去適配,也參考了如下:

https://forums.developer.apple.com/thread/122189
https://stackoverflow.com/questions/58490952/how-to-test-ios-in-use-from-the-javascript-inside-a-wkwebview-on-ipados-13

等等...其他的實在不記得地址了。

但是忙完那段時間之后,想想之前的處理方案并不靠譜,因為蘋果爸爸雖然有不靠譜的地方,但也不至于這樣,所以去查閱了一下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));

在完成適配之后我也查了一下網上的資料,發現很多文章并沒有深究,還是之前那些奇奇怪怪的方案,也可能大佬們懶得總結吧,所以這里分享一下我的解決方案(還是要好好看看api的更新)。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容