Why can#39;t I close or dismiss a Javascript alert in UIWebView?(为什么我不能在 UIWebView 中关闭或关闭 Javascript 警报?)
问题描述
情况:我通过 UIWebView
方法 stringByEvaluatingJavaScriptFromString:
像这样调用 Javascript alert
...
Situation: I invoke a Javascript alert
through the UIWebView
method stringByEvaluatingJavaScriptFromString:
like this...
[myWebView stringByEvaluatingJavaScriptFromString:@"alert('FOOBAR');"];
问题: iOS 按预期显示FOOBAR"提示,但点击关闭"按钮不会关闭提示.
Problem: iOS displays an alert saying "FOOBAR" as expected, but tapping on the "Close" button does not dismiss the alert.
为什么我无法关闭 Javascript 警报?如何让它关闭?
Why can't I close the Javascript alert? How do I get it to close?
推荐答案
这个问题让我对问题有了最深刻的理解……
This question gave me the most insight to the problem...
GCD 和 webView 的死锁
要点是处理 stringByEvaluatingJavaScriptFromString:
方法中的 JS 的线程和处理 iOS 警报视图的线程可能相互阻塞,导致关闭"按钮无响应.
The gist is that the thread handling the JS from the stringByEvaluatingJavaScriptFromString:
method and the thread handling the iOS alert view are probably blocking each other, making the "Close" button unresponsive.
我的解决方法是用 setTimeout
推迟 JS alert
,类似这样...
My workaround is to defer the JS alert
with a setTimeout
, something like this...
NSString *jsMyAlert = @"setTimeout(function(){alert('FOOBAR');}, 1);";
[myWebView stringByEvaluatingJavaScriptFromString:jsMyAlert];
为了避免任何死锁风险,最好让 UIWebView
触发 UIAlertView
而不是依靠 UIWebView
来处理JS 警报
.不过,上述解决方法适用于大多数调试目的.
To avoid any risk of deadlock, it might be better to have the UIWebView
trigger an UIAlertView
rather than rely on UIWebView
to handle the JS alert
. The workaround above would be suitable for most debugging purposes though.
这篇关于为什么我不能在 UIWebView 中关闭或关闭 Javascript 警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么我不能在 UIWebView 中关闭或关闭 Javascript 警报?


基础教程推荐
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01
- 如何使用 YouTube API V3? 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- LocationClient 与 LocationManager 2022-01-01
- :hover 状态不会在 iOS 上结束 2022-01-01
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- 如何使 UINavigationBar 背景透明? 2022-01-01
- 固定小数的Android Money Input 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01