简体   繁体   English

为什么我不能在UIWebView中关闭或关闭Javascript警报?

[英]Why can't I close or dismiss a Javascript alert in UIWebView?

Situation: I invoke a Javascript alert through the UIWebView method stringByEvaluatingJavaScriptFromString: like this... 情况:我通过UIWebView方法stringByEvaluatingJavaScriptFromString:调用Javascript alert stringByEvaluatingJavaScriptFromString:像这样......

[myWebView stringByEvaluatingJavaScriptFromString:@"alert('FOOBAR');"];

Problem: iOS displays an alert saying "FOOBAR" as expected, but tapping on the "Close" button does not dismiss the alert. 问题: iOS按预期显示“FOOBAR”警告,但点击“关闭”按钮不会忽略警报。

Why can't I close the Javascript alert? 为什么我不能关闭Javascript警报? How do I get it to close? 我如何让它关闭?

This question gave me the most insight to the problem... 这个问题让我对这个问题有了最深刻的认识......

Deadlock with GCD and webView 与GCD和webView的死锁

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. 要点是从stringByEvaluatingJavaScriptFromString:方法处理JS的线程和处理iOS警报视图的线程可能相互阻塞,使“关闭”按钮无响应。

My workaround is to defer the JS alert with a setTimeout , something like this... 我的解决方法是使用setTimeout推迟JS alert ,类似这样......

NSString *jsMyAlert = @"setTimeout(function(){alert('FOOBAR');}, 1);";

[myWebView stringByEvaluatingJavaScriptFromString:jsMyAlert];

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 . 为了避免任何死锁风险,让UIWebView触发UIAlertView而不是依靠UIWebView来处理JS alert可能会更好。 The workaround above would be suitable for most debugging purposes though. 上面的解决方法适用于大多数调试目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM