present viewcontroller 出来的h5调用相册问题

问题描述直接借鉴(https://segmentfault.com/q/1010000004528658)segmentfault中的一个描述(

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

或者根据提问者的描述 ViewController A  present  ViewController B 后ViewController B 再push到ViewController C

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模态出来的,验证猜想很简单,在ViewController 重写如下方法即可。

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

我们知道当一个presentedViewController要dismiss的时候会调用presentingViewController的如下方法

断点后发现,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!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值