前陣子適配 iOS13 的時候遇到一個關于WKWebView設置UserAgent的問題实柠,在iPadOS上,WKWebView的UserAgent變成了類似這樣:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko)
這就導致前端根據(jù)UserAgent判斷設備的地方出了問題善涨,當時發(fā)現(xiàn)其主要原因是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嗽交,發(fā)現(xiàn)果然有個新的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拾枣,我們可以按需決定是否實現(xiàn):
/*! @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));
在完成適配之后我也查了一下網上的資料,發(fā)現(xiàn)很多文章并沒有深究盒让,還是之前那些奇奇怪怪的方案梅肤,也可能大佬們懶得總結吧,所以這里分享一下我的解決方案(還是要好好看看api的更新)邑茄。