The adaptive size of the webpage loaded by WKWebView

Sometimes after loading the page in WKWebView, you will find that the words on the page will be very small. This is because the original webpage has not been adapted to the screen size of the mobile phone. So how do we adapt the page on the mobile terminal without making adjustments in the background?
The following Code can be adapted to size
//Lazy Loading
- (WKWebView *)webView {
    if (!_webView) {
        _webView = [[WKWebView alloc] init];
        //The following code adapts the size
        NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
        
        WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        WKUserContentController *wkUController = [[WKUserContentController alloc] init];
        [wkUController addUserScript:wkUScript];
        
        WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
        wkWebConfig.userContentController = wkUController;
        
        _webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:wkWebConfig];
        [self.view addSubview:_webView];
        _webView.navigationDelegate = self;
    }
    return _webView;
}


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326801941&siteId=291194637