Running localhost for local files in WKWebkitView

Hello, I have a local html build which loads content dynamically, so therefore it needs localhost to run. Before when running content through the old webView this was done automatically so no additional setup was needed. However, this is not the case for the newer webkitView.

I load my content:

@IBOutlet weak var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    webView.load(URLRequest(url: URL(fileURLWithPath: Bundle.main.path(forResource: "www/index", ofType: "html")!)))
}

That loads the content fine at first, but does not allow me to load additional content dynamically, probably because their is no localhost server running (which the older depracated webView did automatically).

I’ve also tried adding this to the info.plist file, but it made no difference.

<key>NSAppTransportSecurity</key>  
<dict>  
    <key>NSExceptionDomains</key>  
    <dict>  
        <key>127.0.0.1</key>  
        <dict>  
            <key>NSExceptionAllowsInsecureHTTPLoads</key>  
            <true/>  
        </dict>  
        <key>localhost</key>  
        <dict>  
            <key>NSExceptionAllowsInsecureHTTPLoads</key>  
            <true/>  
        </dict>  
    </dict>  
</dict> 

Does anybody know how to get a localhost server running on WKWebitView???

Many thanks in advance.

Hi @deniznasif,
The localhost is applicable only if you are running a webserver on the device. Your page loads ups fine, I did not quite understand what you mean by does not allow you to load additional content. Are the resources (.js scripts, .css and images) not loaded or are you loading them from a website somewhere?

You do not need to run a webserver to load local resources as long as they are all in the resource directory and ensure that they are all included in the resources under the build phases, ensure that the resources are copied/bundled.

cheers,

Jayant

Hi @jayantvarma

Yes all js and css is loading fine. Basically, I am loading content dynamically. So for example, I have a welcome screen which I then click to display the menu (this works and looks fine, so I know my js script and css is loading fine). Then when I click/tap on a menu item, I use a json file to dynamically load the correct content, it is this part that does not work. I know the script is fine because it works when I run it in a desktop browser and it also worked fine in iOS when I use the older depracated webView. It just doesn’t work with WKWebkitView. I obviously want to get this to work in WKWebkitView before the older webView is phased out of Swift permanently.

Thanks.

Hi @deniznasif,
Generally it works, if you provide some code snips it would be easier to see what is happening.

What functions of WKWebViewDelegate have you implemented? The decidePolicyFor:decisionHandler is important even though it is mentioned as optional in the docs.

https://developer.apple.com/documentation/webkit/wknavigationdelegate

cheers,

Jayant

Hello again @jayantvarma

Apart from importing WebKit, it literally is just the code within viewDidLoad in my first post. The WKWebkitView itself was added via Storyboard. No WKWebViewDelegate has been implemented. I guess it would be worth looking into.

Hi @deniznasif, are you only having issues with loading a json file with WKWebkitView? I have written some example code that successfully loaded an html and json file. I used a WKWebView with the storyboard. Hope this helps!

import UIKit
import WebKit

class ViewController: UIViewController {

@IBOutlet weak var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()
    
    do {
        guard let filePath = Bundle.main.path(forResource: "example_1", ofType: "json")
            else {
                // File Error
                print ("File reading error")
                return
        }
        
        let contents =  try String(contentsOfFile: filePath, encoding: .utf8)
        let baseUrl = URL(fileURLWithPath: filePath)
        webView.loadHTMLString(contents as String, baseURL: baseUrl)
    }
    catch {
        print ("File JSON error")
    }
}

}

Best,
Gina

This topic was automatically closed after 166 days. New replies are no longer allowed.