我在一个使用UIWebView的应用程序中实现了wkwebview。当指向嵌入了javascript的本地html文件时,我无法让javascript执行。javascript被剥离成一个简单的警告并加载一个基本的google地图。所有这些都不会被执行。我需要运行本地服务器吗?GCDWebserver..
我要补充的是,html/javascript在safari中工作,谷歌浏览器没有问题。
尝试的解决方案包括: 1.加载AppTransportSecuritySettings AllowArbitrary。2. javascript webview.configuration.preferences.javaScriptEnabled = true 3.这个问题解决了这个问题,并说这个问题已经在iOS 10.3 Load Javascript files through HTML file on WKWebView in iOS中解决了,模拟器正在运行12.1 4。这个问题还解决了这个问题,回答是要求GCDWebserver能够使用wkwebview执行javascript。然而,这个问题也在iOS的最终版本中得到了解决。下面是一些代码:
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
//@IBOutlet var googleMap: WKWebView!
var webview: WKWebView!
override func loadView() {
webview = WKWebView()
webview.navigationDelegate = self
view = webview
}
override func viewDidLoad() {
super.viewDidLoad()
//let url = URL(string: "https://schallerf1.com")!
let url = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "www")!
webview.load(URLRequest(url: url))
webview.allowsBackForwardNavigationGestures = true
let request = URLRequest(url: url)
webview.configuration.preferences.javaScriptEnabled = true
webview.load(request)
}
}
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<b>WHYYYYYYYYYY!!!!</b>
<div style="height:100%;width:100%;" id="map"></div>
<script type="text/javascript">
var name = "TESTTESTTEST";
alert('code: ' + name + '\n');
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 39.976068, lng: -83.003297},
zoom: 8
});
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=xxxxxxxxxxxxxxxxxx&callback=initMap"></script>
</body>
</html>
所有的javascript都不起作用,我应该得到一个警告和一个简单的谷歌地图应该会显示。我需要查看本地web服务器GCDWebserver吗?
发布于 2019-06-14 01:29:23
您应该在此WKNavigationDelegate
方法中调用javascript,该方法在webview完成加载时调用。
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("initMap()", completionHandler: { (value, err) in
// Handle response here.
})
}
另外,我不确定为什么要调用两个不同的webview.load()
请求--也许不要这样做?
https://stackoverflow.com/questions/56583506
复制相似问题