dejix
dejix

Reputation: 1144

Load local HTML/Javascript with WKWebView iOS

I try to load a local HTML game with javascript on WKWebView. Although i can see HTML pages styled by the CSS, I can't hear any sound of the game and some game's actions not corresponding.

I notice that if i load exactly the same game via http live server everything seems perfect. So I suppose that something goes wrong with local load. Maybe this one Apache's issue could be the problem?

Info.plist :

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

My code (SWIFT 3) :

import UIKit
import WebKit

class WebViewController: AppViewController, WKUIDelegate {

    var webView: WKWebView!

    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        if let path = Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "game") {
            webView.load(URLRequest(url : URL(fileURLWithPath: path)))
        }
    }

}

Upvotes: 4

Views: 3676

Answers (1)

Arnaud Wurmel
Arnaud Wurmel

Reputation: 500

In order to play JavaScript in local, you must run a local webserver with GCDWebServer. You can get doc here. Apple is blocking JavaScript execution without a WebServer

Upvotes: 4

Related Questions