user1391152
user1391152

Reputation: 1289

Running localhost for WKWebkitView

Previously, when loading local html content into UIWebView, it would automatically run localhost/server in the background. This server emulation would enable me to load dynamic content through json, for example. Example code below;

@IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

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

I am now trying to implement this into a WKWebView. I can load local html content, but unlike UIWebView, WKWebView does not emulate localhost/server, so I cannot do things like before, such as dynamically load content with json etc. How would I go about running local html content through localhost? If UIWebView had that feature automatically, surely WKWebView should have it right? Code below.

@IBOutlet weak var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

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

Note: I am using Xcode 9 for this, so WKWebview is being added through Storyboard and the referenced as an outlet.

Many thanks in advance to anybody who can help me with this.

Upvotes: 4

Views: 7846

Answers (1)

Cyber
Cyber

Reputation: 2724

Add below to your info.plist and start the localhost server

<key>NSAppTransportSecurity</key>  
<dict>  
    <key>NSExceptionDomains</key>  
    <dict>  
        <key>127.0.0.1:80</key>  
        <dict>  
            <key>NSExceptionAllowsInsecureHTTPLoads</key>  
            <true/>  
        </dict>  
        <key>localhost:port</key>  
        <dict>  
            <key>NSExceptionAllowsInsecureHTTPLoads</key>  
            <true/>  
        </dict>  
    </dict>  
</dict> 

Upvotes: 10

Related Questions