A界面为Webview,B界面扫描二维码后,返回A界面,直接弹出JS的Alert,Alert的确定无法响应

A界面为Webview,B界面扫描二维码后,返回A界面,直接弹出JS的Alert,Alert的"好"无法响应


网上的解释

This question gave me the most insight to the problem...

Deadlock with GCD and 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.

My workaround is to defer the JS alert with a setTimeout, something like this...

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. The workaround above would be suitable for most debugging purposes though.

注意setimeout中的第一个参数必须是function  不能直接alert  否则还是会lock

解决办法

[webview performSelectorOnMainThread: @selector(stringByEvaluatingJavaScriptFromString:) withObject: jsString waitUntilDone: NO];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 关于UIWebView的介绍,相信看过上文的小伙伴们,已经大概清楚了吧,如果有问题,欢迎提问。 本文是本系列...
    CoderLF阅读 9,090评论 2 12
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,642评论 2 45
  • iOS 的 Cookie 存取 https://juejin.im/entry/58d4c4cc44d904006...
    Farmers阅读 6,053评论 0 16
  • 一、WebView WebView就是一个内嵌浏览器控件,在iOS中主要有两种WebView:UIWebView和...
    iOS祎阅读 1,157评论 0 2
  • 笑容可以瞒过别人,心疼却瞒不过自己!
    样儿r阅读 107评论 4 3