4

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)))
        }
    }

}

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Do you know how is it possible with GCDWebServer to load whole local website with JS and not just a simple html file?
I guess yes, I use it in order to load multiple html file in my project. So, you can probably do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.