Need to Inject HTML into Body tag of WKWebView


#1

Hello,

I have a WKWebView that loads a defaultSummary.html file. It’s pretty basic. Before it loads, I need to somehow replace the contents of the “” tag with new custom HTML data.

Any assistance appreciated.

defaultSummary.html:




Summary Preview

    </head>
    <body id="summary-body">
        
        REPLACE THIS WITH CUSTOM DATA
        
        <table border="1" cellpadding="1" cellspacing="1" style="width:100%"><tbody><tr><td>STrength</td><td>Weakness</td></tr><tr><td>wqfef</td><td>fqefqe</td></tr><tr><td>qfwqw</td><td>qfqfw</td></tr><tr><td>qfwqwf</td><td>qwfqwfqfw</td></tr></tbody></table>

    </body>
</html>

#2

Hello guys,

I figured it out.

Below is my code. I added multiple scripts to test that you could change the previous added data, and you can.

-(void)setupWebViewInView:(UIView *)view {
    
    /// test - start
    
    NSString *sourceTest = @"var summarybody = document.body; \
    summarybody.innerHTML = '<strong>FRESH</strong> <div id=\"testarea\">TEST-1-2-3</div>'; \
    summarybody.style.background = \"#777\";";
    WKUserScript *test = [[WKUserScript alloc] initWithSource:sourceTest injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    
    NSString *sourceTestArea = @"var area = document.getElementById('testarea'); \
    area.innerHTML = '<strong>TEST-1-2-3-4-5-6-7-8</strong>';";
    WKUserScript *testArea = [[WKUserScript alloc] initWithSource:sourceTestArea injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    
    // test - end
    
    // Javascript that disables pinch-to-zoom by inserting the HTML viewport meta tag into <head>
    // This helps the CKEditor load with proper scale
    NSString *source = @"var meta = document.createElement('meta'); \
    meta.name = 'viewport'; \
    meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no'; \
    var head = document.getElementsByTagName('head')[0];\
    head.appendChild(meta);";
    WKUserScript *script = [[WKUserScript alloc] initWithSource:source injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    
    // Create the user content controller and add the script to it
    WKUserContentController *userContentController = [[WKUserContentController alloc]init];
    [userContentController addUserScript:script];
    [userContentController addUserScript:test];
    [userContentController addUserScript:testArea];
    
    // Create the configuration with the user content controller
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]init];
    configuration.userContentController = userContentController;
    
    CGRect webViewFrame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
    
    // Create the web view with the configuration
    self.webView = [[WKWebView alloc] initWithFrame:webViewFrame configuration:configuration];
    
    self.webView.navigationDelegate = self;
    [view addSubview: self.webView];
    
}