在 UIWebView 中调试 Javascript 的方法有哪些?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2767902/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:51:04  来源:igfitidea点击:

What are some methods to debug Javascript inside of a UIWebView?

javascriptiphonehtmluiwebview

提问by bpapa

I'm trying to figure out why something with Javascript isn't working inside of a UIWebView. To my knowledge, there is no way to set a breakpoint inside of XCode for a js file. No problemo, I'll just go back to 2004 and use alert statemen-- oh wait they don't seem to work inside of a UIWebView either!

我试图弄清楚为什么在 UIWebView 中使用 Javascript 的东西不起作用。据我所知,无法在 XCode 内部为 js 文件设置断点。没问题,我会回到 2004 年并使用警报语句——哦等等,它们似乎也不能在 UIWebView 内工作!

The only thing I could think of is by exporting my HTML And JS files to my desktop and then just doing my debugging inside of Safari. And that works! But of course, the bug I'm fighting with in the UIWebView doesn't occur in Safari.

我唯一能想到的就是将我的 HTML 和 JS 文件导出到我的桌面,然后在 Safari 中进行调试。这有效!但是当然,我在 UIWebView 中解决的错误不会出现在 Safari 中。

Are there any other ways for debugging inside of a UIWebView, or any tricks that I can use akin to using the old-school alert method?

有没有其他方法可以在 UIWebView 内部进行调试,或者我可以使用类似于使用老式警报方法的任何技巧?

回答by refaelos

If you're using iOS >= 6 and you have mountain lion (10.8) or Safari >= 6, you can just:

如果您使用的是 iOS >= 6 并且您有 Mountain Lion (10.8) 或 Safari >= 6,您可以:

  1. Open the application in the simulator (or your device in XCode >= 4.5.x).
  2. Open Safari (go to Preferences -> Advancedand make sure "Show Develop Menu in Menubar" is on.
  3. From the Menu-bar (of Safari) select Develop -> iPhone Simulator -> [your webview page].
  1. 在模拟器中打开应用程序(或在 XCode >= 4.5.x 中打开您的设备)。
  2. 打开 Safari(转到Preferences -> Advanced并确保“在菜单栏中显示开发菜单”已打开。
  3. 从(Safari 的)菜单栏中选择Develop -> iPhone Simulator -> [your webview page]

That's it !

就是这样 !

回答by Dan Bennett

This query tops google, so worth linking to the remoteInspector hidden in iOS5- by far the best way found so far to debug your UIWebViews - just conditional compile out before you send to Apple.

这个查询在 google 上名列前茅,因此值得链接到隐藏在 iOS5 中remoteInspector- 迄今为止发现的调试 UIWebViews 的最佳方法 - 在发送给 Apple 之前有条件地编译出来。

回答by rob

alert() certainly works for me.

alert() 当然对我有用。

However, you can also do lots of other things, like make your own DHTML alert that pops up in a layer. This can be nice because you can do multiple alerts to a single div, without stopping your app. You should also be able to write a stack trace to it (the stack trace is in the exception object, and you can always throw your own exceptions).

但是,您还可以做很多其他事情,例如制作自己的 DHTML 警报,在图层中弹出。这很好,因为您可以对单个 div 发出多个警报,而无需停止您的应用程序。您还应该能够向其写入堆栈跟踪(堆栈跟踪位于异常对象中,您始终可以抛出自己的异常)。

Alternatively, if running on the simulator your custom "alert()" could call into objective C, and display the string in the iPhone simulator's console window:

或者,如果在模拟器上运行,您的自定义“alert()”可以调用目标 C,并在 iPhone 模拟器的控制台窗口中显示字符串:

document.location.href = "http://debugger/" + 
   encodeURIComponent(outputString);   

and on the objective C side:

在目标 C 方面:

//--------------------------------------------------------------------
- (BOOL)webView:(UIWebView*)webView 
       shouldStartLoadWithRequest: (NSURLRequest*)req 
       navigationType:(UIWebViewNavigationType)navigationType {
    if ([[[req URL] host] isEqualToString:@"debugger"]){
        // do stuff with [[req URL] path] 
       }
}

That said, I have an app that is heavy on the UiWebView / javascript stuff, and I tend to do most javascript dev in Chrome (simulating what is necessary from the iPhone environment)

也就是说,我有一个应用程序在 UiWebView / javascript 方面很重要,而且我倾向于在 Chrome 中进行大多数 javascript 开发(模拟 iPhone 环境中的必要内容)

回答by Adrian Harris Crowne

I haven't tried this yet, but take a look at this Weinre

我还没有试过这个,但看看这个Weinre

Looks very promising.

看起来很有前途。

回答by Anup Gupta

I get the awesome way to debug UIWebViewOr SFSafariViewController.

我得到了调试UIWebViewSFSafariViewController的绝妙方法。

I hope It will Help.

我希望它会有所帮助。

Step 1:? Open Safari VC In Your Mac (hahaha Don't make your face, If I am saying in your Macbook just follow this my steps)??

第 1 步:? 在您的 Mac 中打开 Safari VC(哈哈,不要脸,如果我在您的 Macbook 中说,请按照我的步骤操作)??

Step2:Go at Safari preferences And Click on Advance. ??You will Get this setting on your MacBook Screen.

步骤 2:进入 Safari 首选项并单击高级。??您将在 MacBook 屏幕上获得此设置。

enter image description here

在此处输入图片说明

Now enable the Show to develop menu in menu bar.?? Now Your All work is done. ??Are you thinking I am kidding :P :P no man... ??

现在在菜单栏中启用 Show to develop 菜单。?? 现在你的所有工作都完成了。??你以为我在开玩笑:P :P 没有人... ??

Step3:Run your application in Device or Simulator. (Don't Think Just run )?And go in your application where you are opening your Webview or SFSafariViewController. ? Till now you did not understand I know. Be cool and see my next step.

步骤 3:在设备或模拟器中运行您的应用程序。(不要认为只是运行)?然后进入您打开 Webview 或 SFSafariViewController 的应用程序。? 直到现在你不明白我知道。冷静点,看看我的下一步。

?Step4:Open Safari In your MacBook and Click on Develop Option from the Menu bar. ??Did you get something your MacBook, iPad/ iPhone is Displaying Right?????

? 第 4 步:在 MacBook 中打开 Safari,然后单击菜单栏中的“开发选项”。??您的 MacBook、iPad/iPhone 显示正确了吗????

enter image description here

在此处输入图片说明

??Step5:Its Done. click your device and click on URL New popup will come out like This. enter image description here

?? 第5步:完成。单击您的设备并单击 URL 新弹出窗口将像这样出现。 在此处输入图片说明

???Step6:what are you looking now its over here all the steps.

???Step6:你现在在看什么,所有步骤都在这里。

Now debug your Webpage on this console. Be happy and enjoy your day while doing coding With a cup of tea or Coffee.

现在在此控制台上调试您的网页。一边喝茶或喝咖啡一边编码,一边快乐和享受你的一天。

IMP: Don't forget to enable See Below Imageenter image description here???????

IMP:不要忘记启用见下图在此处输入图片说明???????

回答by Kazekage Gaara

This is an old question. But I'll still like to share my two cents.

这是一个老问题。但我还是想分享我的两分钱。

I have been using jsconsole.comfor remote debugging. It's easy. Just include a script tag and use console logs to debug by printing. This can also be used for debugging on a real device.

我一直在使用jsconsole.com进行远程调试。这很简单。只需包含一个脚本标签并使用控制台日志通过打印进行调试。这也可用于在真实设备上进行调试。

回答by talsibony

Old question, but I think I found the best solution, in my case you need to debug uiwebview, but I had no access to the IOS app and only to the html content and I had to view some JS logs, I added the following code to load the light firebug JS and show it automatically:

老问题,但我想我找到了最好的解决方案,在我的情况下,您需要调试 uiwebview,但我无法访问 IOS 应用程序,只能访问 html 内容,我不得不查看一些 JS 日志,我添加了以下代码加载轻型萤火虫 JS 并自动显示:

calling it from JS

从 JS 调用它

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://getfirebug.com/firebug-lite-debug.js';
document.head.appendChild(script);

or load it from html

或从 html 加载它

<script type="text/javascript" src="https://getfirebug.com/firebug-lite-debug.js"></script>

回答by drawnonward

You can set up a system like that used in PhoneGapto send messages from JavaScript to Objective-C and log from there. In a nutshell, they are setting document.location with a custom scheme and blocking that scheme in the delegate callback.

您可以设置一个类似于PhoneGap中使用的系统,将消息从 JavaScript 发送到 Objective-C 并从那里记录。简而言之,他们使用自定义方案设置 document.location 并在委托回调中阻止该方案。

You can take advantage of the fact that a UIWebView is most of the delegates for a WebView. WebKit is technically undocumented for iPhone, but mostly the same as specified in the desktop WebKit headers, possibly including the WebScriptObject. I use this for debugging, but be sure to strip this code out before submitting to Apple.

您可以利用 UIWebView 是 WebView 的大部分委托这一事实。WebKit 在技术上没有用于 iPhone,但大部分与桌面 WebKit 标头中指定的相同,可能包括 WebScriptObject。我用它来调试,但一定要在提交给 Apple 之前去掉这段代码。

To get a WebView from a UIWebView, implement a delegate method like -(void) webView:(id)inWebView didFinishLoadForFrame:(id)inWebFramein a subclass of UIWebView. Call super if you use that one.

要从 UIWebView 获取 WebView,请在 UIWebView-(void) webView:(id)inWebView didFinishLoadForFrame:(id)inWebFrame的子类中实现一个委托方法。如果您使用那个,请致电超级。