简体   繁体   English

使用WKWebView iOS加载本地HTML / Javascript

[英]Load local HTML/Javascript with WKWebView iOS

I try to load a local HTML game with javascript on WKWebView. 我尝试在WKWebView上使用javascript加载本地HTML游戏。 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. 虽然我可以看到由CSS设计的HTML页面,但我听不到游戏的任何声音和一些游戏的动作不对应。

I notice that if i load exactly the same game via http live server everything seems perfect. 我注意到,如果我通过http直播服务器加载完全相同的游戏,一切似乎都很完美。 So I suppose that something goes wrong with local load. 所以我认为本地负载出了问题。 Maybe this one Apache's issue could be the problem? 也许这一个Apache的问题可能是问题?

Info.plist : Info.plist:

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

My code (SWIFT 3) : 我的代码(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. 要在本地播放JavaScript,您必须使用GCDWebServer运行本地Web服务器。 You can get doc here . 你可以在这里获得doc。 Apple is blocking JavaScript execution without a WebServer Apple在没有WebServer的情况下阻止了JavaScript的执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 WKWebView(iOS、Swift)中使用本地 Javascript 源加载本地 HTML - Loading local HTML with local Javascript Sources in a WKWebView (iOS, Swift) 在iOS的WKWebView上通过HTML文件加载Javascript文件 - Load Javascript files through HTML file on WKWebView in iOS iOS:WKWebView 不会立即加载本地 JS 文件 - iOS: WKWebView does not load local JS files immediately WKWebView JavaScript在页面加载时的通信 - WKWebView JavaScript communication on page load iOS -WKWebView - 从本地主机目录/文件(.js 和资产)加载内容 - iOS -WKWebView - load content from local host directory/files (.js and assets) WKWebView iOS 链接 Javascript 文件 - WKWebView iOS linking Javascript File 如何拦截 WKWebView 请求以检测哪些本地资源文件(css、js、png、...)与 HTML 文件一起加载? - How to intercept a WKWebView request to detect which local resource files (css, js, png, …) load together with a HTML file? WKWebView加载使用sea.js和avalon的本地html5项目 - WKWebView load local html5 project which use sea.js and avalon 通过在WKWebView中评估JavaScript来获取HTML - Get HTML by evaluating JavaScript in a WKWebView 无法在wkwebview中运行html中嵌入的javascript - javascript embedded in html not running in wkwebview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM