iPad iOS16+ Can't load "https://www.baidu.com/s?wd=123" in WKWebView

iPad WKWebView can't load some pages on "https://www.baidu.com" website e.g. "https://www.baidu.com/s?wd=123"

When load 'https://www.baidu.com/s?wd=123' in wkwebview, some wkwebview.navigationDelegate methods are always called in a loop

And this problem occurs in iPad iOS16+

use this code can solve the problem, I found it from apple sample code, and I have the same problem on MacOS

  • (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction preferences:(WKWebpagePreferences *)preferences decisionHandler:(void (^)(WKNavigationActionPolicy, WKWebpagePreferences *))decisionHandler{ [preferences setPreferredContentMode:WKContentModeMobile]; decisionHandler(WKNavigationActionPolicyAllow, preferences);

}

here is the apple sample code of WKWebView, if you enter https://www.baidu.com, the problem appears, if you change it to mobile mode, it will be ok

I did two things to solve this problem, here is the example code:

		let webConfiguration = WKWebViewConfiguration()
		if UIDevice.current.userInterfaceIdiom != .phone {
			let prefer = WKWebpagePreferences()
			prefer.preferredContentMode = .mobile
			webConfiguration.defaultWebpagePreferences = prefer
		}
		
		webView = WKWebView(frame: .zero, configuration: webConfiguration)
		if UIDevice.current.userInterfaceIdiom != .phone {
			webView.customUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15"
		}
iPad iOS16+ Can't load "https://www.baidu.com/s?wd=123" in WKWebView
 
 
Q