最新消息:iOS编程开发交流群(6906921) ,Mac.Cocoa开发交流群(7758675) 欢迎iOS/macOS开发编程爱好及学习者加入!

模态UIViewController中WebView的H5弹出Camera/ImagePicker

iOS 天狐 19698浏览 3评论

问题描述直接借鉴(https://segmentfault.com/q/1010000004528658)segmentfault中的一个描述(虽然我及其讨厌使用ViewController A  B C来描述问题),因为我没有对应账号,没法回答它。

情况大致就是一个ViewController A  present  ViewController C,

或者根据提问者的描述 ViewController A  present  ViewController B 后ViewController B 再push到ViewController C(本文章的Demo也是根据这个来做的)

ViewController C中有一个WebView,加载了H5页面, H5页面有个按钮调用手机的相册 UIImagePickerController,弹出了拍照,相册选择的UIActionSheet, 然后点击相册按钮,程序直接退出到了 A 页面。

bvtar12.提问者尝试用A push 到B 页面,B页面push 到C 页面,这样子的话,在点击相册后会到系统相册里面,选择图片后会返回到H5页面里。

3.提问者需要的是present 也能实现而不是用push。所以尝试了以下方法

bvtar1234结论就是只要有present ,就是出现问题,而且提示问题就出现在present 那里

为什么这样?

74d154e3-f37d-4696-b910-a8248b789eed虽然我们看不到点击h5弹出的UIActionSheet(暂且称作UIActionSheet),点击方法的实现。但是我们从UI交互上来看是这样的流程。

点击UIActionSheet中item  => UIActionSheet dismiss => 弹出相机或者ImagePicker => 拍照或者选择图片后弹出相机或者ImagePicker dismiss

科普

presentingViewController :The view controller that presented this view controller (or its farthest ancestor.  当前ViewController present出来的ViewController

presentedViewController:The view controller that was presented by this view controller or its nearest ancestor. present当前ViewController的ViewController

分析

我们知道UIActionSheet可能是被当前ViewController C执行presentViewController模态出来的

也有可能是被C的parentViewControler(这里就是NavgationControler了)模态出来的,验证猜想很简单,在ViewController C,NavgationControler子类,重写如下方法即可。

断点发现会走ViewController C的presentViewController

断点后发现弹出的viewControllerToPresent并不是UIActionSheet,而是我们很少使用的UIDocumentMenuViewController,然后并没有什么卵用,尝试hook UIDocumentMenuViewController点击方法失败。

我们知道当一个presentedViewController要dismiss的时候会调用presentingViewController的如下方法,所以重写ViewController C中dismissViewControllerAnimated

断点后发现,dismissViewControllerAnimated执行了两次,我们的bug现象就是,dimiss掉了所有的modal试图,那么好了,问题找到了,UIDocumentMenuViewController关闭后不仅调用了自己的,dismissViewControllerAnimated,还调用了,上层或者上上层presentingViewController的dismissViewControllerAnimated。

所以使dismissViewControllerAnimated调用一次,或者让UIDocumentMenuViewController找不到presentingViewController即可。

如何解决?

方法一,使dismissViewControllerAnimated只调用一次

当前ViewController 的所有presentedViewController都正常执行dismissViewControllerAnimated,当前ViewController本身执行dismissViewControllerAnimated,不进行dismiss,不做处理。

当想dismiss掉当前ViewController的时候,不能调用

而是使用

即可,这样当前ViewController不会调用重写的dismissViewControllerAnimated方法,而且还顺利的dismiss掉了。

方法二,使UIDocumentMenuViewController找不到presentingViewController

断点后发调用

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag

弹出UIDocumentMenuViewController之后,又调用了

- (UIViewController *)presentingViewController

返回的presentingViewController是ViewController A,

测试发现调用ViewController A的dismissViewControllerAnimated之后,所有model出来的ViewController 都会dismiss。这也是苹果的一个特性。当模态出N个ViewController之后,只需要dismiss任意一个,都会dismiss它之后的所有模态试图

0f11d0b9-786d-4dbb-91ae-30710e76d18f

解决方法就是不让UIDocumentMenuViewController找到上层或者上上层的任意presentingViewController

Done!

 

20170930更新

后来遇到其他项目,断点发现会走NavgationControler C的presentViewController,viewControllerToPresent依然是UIDocumentMenuViewController没有变化。

断点NavgationControler子类的dismissViewControllerAnimated,同样也是调用了两次。

第一次调用

第一次调用我们发现是AlertControler(ActionSheet模式应该是)调用了dismiss。

第二次调用我们发现是UIDocumentMenuViewController调用了dismiss。

尝试之前的方法在CustomNavigationController中,虽然也会好用,但是其他正常dismiss可能遇到问题。

暂时还没有好的解决方案。

demo地址

https://github.com/shaojiankui/H5-Camera-ImagePicker-Demo

转载请注明:天狐博客 » 模态UIViewController中WebView的H5弹出Camera/ImagePicker

微信 OR 支付宝 扫描二维码
为天狐 打赏
非常感谢你的支持,哥会继续努力!
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (3)

  1. 4声望。哈哈
    kkkelicheng8年前 (2016-12-12)回复
  2. 这个为什么会调用2次,是不是系统bug
    kkkelicheng8年前 (2016-12-12)回复
  3. 您好,最近遇见这个问题。看了你的文章分析很好让我受益匪浅。关于您说的影响其他的dismiss,尝试了一下发现NavgationController继承与UIViewContrller,所以我试着重写了viewWillAppear在里面重置你的_flag,这样就解决掉了误伤问题。
    不是本人6年前 (2018-02-01)回复