如何在 WKWebView 中调试 javascript 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25871586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to debug javascript code inside a WKWebView
提问by overactor
I'm having some trouble with running a webapp in a WKWebView (specifically: some buttons are not responding). I used this tutorialas a guide and did succeed in having the webview display my webapp. So now I want to debug the javascript code.
我在 WKWebView 中运行 web 应用程序时遇到了一些问题(特别是:某些按钮没有响应)。我使用本教程作为指南,并成功地让 webview 显示了我的 webapp。所以现在我想调试javascript代码。
I know the webapp works, since I've tried it in both an android webview as well as countless browsers (including safari on the iPad simulator I'm using). After some quick googling, I found out how to debug javascript inside a UIWebView using Safari. Sadly This doesn't seem to work with the new WKWebView.
我知道 webapp 可以工作,因为我已经在 android webview 以及无数浏览器(包括我正在使用的 iPad 模拟器上的 safari)中尝试过它。经过一些快速的谷歌搜索,我发现了如何使用 Safari 在 UIWebView 中调试 javascript。遗憾的是,这似乎不适用于新的 WKWebView。
When I navigate to Develop->iPad SimulatorI'm told that there are 'No Inspectable Applications'. I've tried the very same thing with a simple app with a UIWebView and debugging through safari works perfectly there.
当我导航到Develop->iPad Simulator我被告知有“没有可检查的应用程序”。我已经用一个带有 UIWebView 的简单应用程序尝试了同样的事情,并且通过 safari 调试在那里完美地工作。
Is there a way to debug javascript code within a WKWebView?
有没有办法在 WKWebView 中调试 javascript 代码?
To reproduce my problems (using swift), start a new single screen project in xCode 6, copy the code (courtesy of kinderas) provided below in your ViewController.swiftfile and drag the outlet to Viewunder View Controllerin the document outline in the Main.storyboardfile. Refer to the tutorial I linked above in case of confusion.
要重现我的问题(使用 swift),请在 xCode 6 中启动一个新的单屏幕项目,将下面提供的代码(由kinderas提供)复制到ViewController.swift文件中,然后将插座拖到文件中文档大纲的View下方。如有混淆,请参阅我上面链接的教程。View ControllerMain.storyboard
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet var containerView : UIView! = nil
var webView: WKWebView?
override func loadView() {
super.loadView()
self.webView = WKWebView()
self.view = self.webView!
}
override func viewDidLoad() {
super.viewDidLoad()
var url = NSURL(string:"http://www.kinderas.com/")
var req = NSURLRequest(URL:url)
self.webView!.loadRequest(req)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
回答by a.stringham
UPDATE: 2017/09/11
更新:2017/09/11
Recent versions of Safari can do this. Just enable the develop menu in Safari - developer.apple.com
最新版本的 Safari 可以做到这一点。只需在 Safari 中启用开发菜单 - developer.apple.com
OLD:
老的:
You need to install the nightly build of Safariand use that. I was having the same problem with an app I'm developing. Once I installed the nightly build, I now use that and all of my WKWebViews show up in the develop menu.
您需要安装Safari的夜间版本并使用它。我正在开发的应用程序遇到了同样的问题。一旦我安装了夜间构建,我现在使用它并且我所有的 WKWebViews 都显示在开发菜单中。
Hope that's the part you were missing!
希望那是你遗漏的部分!
回答by Biribu
You can try http://jsconsole.com/
Execute :listen and it gives you a script tags to paste in your webpage.
执行 :listen ,它会为您提供一个脚本标签以粘贴到您的网页中。
Every console.log you have in that page will give their output to this page.
您在该页面中的每个 console.log 都会将其输出提供给该页面。
If you close jsconsole page, next time you should change script src in html code but you can listen to that script src too:
如果您关闭 jsconsole 页面,下次您应该更改 html 代码中的脚本 src 但您也可以收听该脚本 src:
:listen XXXXX-XXXXX-XXXX-XXXXX
I hope it helps.
我希望它有帮助。
回答by ????
open the website or html page in your webview, then open safari. click develop, then go to simulator and you will find your website title there. once you open it you will see the javascript debugger, console and everything else you can dream of.
在 webview 中打开网站或 html 页面,然后打开 safari。单击开发,然后转到模拟器,您将在那里找到您的网站标题。打开它后,您将看到 javascript 调试器、控制台以及您梦寐以求的一切。

