嵌入在 html 中的 javascript 未在 wkwebview 中运行 [英] javascript embedded in html not running in wkwebview

查看:29
本文介绍了嵌入在 html 中的 javascript 未在 wkwebview 中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UIWebView 的应用程序中实现 wkwebview.当指向一个嵌入了 javascript 的本地 html 文件时,我无法让 javascript 执行.javascript 被剥离来做一个简单的警报并加载一个基本的谷歌地图.这些都没有得到执行.我需要运行本地服务器吗?GCDWebserver..

I am implementing wkwebview in an app that was using UIWebView. I am unable to get javascript to execute when pointing to a local html file that has the javascript embedded within it. The javascript is stripped to do a simple alert and load a basic google map. None of that is getting executed. Do I need to run a local server? GCDWebserver..

我应该补充一下,html/javascript 可以在 safari 中运行,谷歌浏览器没问题.

I should add, the html/javascript works in safari, google browser no problem.

尝试的解决方案包括:1. AppTransportSecuritySettings 允许任意加载.2. ViewController.swift webview.configuration.preferences.javaScriptEnabled = true3. 这个问题解决了这个问题,并说是在 iOS 10.3 在iOS的WKWebView上通过HTML文件加载Javascript文件 模拟器运行的是12.14. 这个问题也解决了要求 GCDWebserver 能够使用 wkwebview 执行 javascript 的问题.WKWebView 不执行任何 js 代码 然而,这也在最新版本的IOS.这是一些代码:

Solutions attempted include: 1. AppTransportSecuritySettings AllowArbitrary loads. 2. ViewController.swift webview.configuration.preferences.javaScriptEnabled = true 3. This question addresses the issue and says is was fixed in iOS 10.3 Load Javascript files through HTML file on WKWebView in iOS The simulator is running 12.1 4. This question also addresses the issue with an answer of requiring GCDWebserver to be able to execute javascript using wkwebview. WKWebView not executing any js code This however was also solved in a laster version of iOS. Here is some code:

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 吗?

None of the javascript works, I should get an alert and a simple google map should display. Do I need to look into the local web server GCDWebserver?

推荐答案

您应该在这个 WKNavigationDelegate 方法中调用您的 javascript,该方法在 webview 完成加载时调用.

You should be calling your javascript in this WKNavigationDelegate method, which is called when the webview finishes loading.

public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    webView.evaluateJavaScript("initMap()", completionHandler: { (value, err) in
        // Handle response here.
    })
}

另外,我不确定您为什么要调用两个不同的 webview.load() 请求 - 也许不这样做?

Also, I'm not sure why you're calling two different webview.load() requests - maybe don't do that?

这篇关于嵌入在 html 中的 javascript 未在 wkwebview 中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆