iOS Using WKWebView to Embed a Viewing Page, Displaying as PC Viewing Page
Updated: 2023-03-26 20:05:59
Problem Description
In an iOS native app (iPad), after using WKWebView to embed the Polyv SaaS viewing page https://live.polyv.cn/watch/{channeId}, the displayed page appears as the PC version.
Cause and Solution
- Apple modified the userAgent rules for iPad starting from iOS 13. Without specific configuration, the device is recognized as a PC desktop system, resulting in the display of the PC version page.
- In the iPad device environment, configuring the mobile mode resolves the issue. The specific code is as follows:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction preferences:(WKWebpagePreferences *)preferences decisionHandler:(void (^)(WKNavigationActionPolicy, WKWebpagePreferences *))decisionHandler {
if (@available(iOS 13, *)) {
preferences.preferredContentMode = WKContentModeMobile;
}
}
