简体   繁体   中英

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

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM