一起养成写作习气!这是我参加「日新计划 4 月更文应战」的第3天,点击查看活动详情。
前语
Avoiding iOS bug:UIWebView with file input doesn‘t work in modal view controller
- 从CSDN下载demo https://download.csdn.net/download/u011018979/15133282
github.com/zhangkn/KNU…
- 使用场景:modal UIWebView and camera/image picker
因为苹果模态的特性,当模态出N个ViewController之后,只需要dismiss恣意一个,都会dismiss它之后的一切模态企图 。 因而特性会导致的问题:
modal模态出来的UIViewControllerWebView的H5弹出Camera/ImagePicker 时,当UIDocumentMenuViewController消失的时候会导致WebView 所在的控制器也被干掉。
- 处理思路:使dismissViewControllerAnimated调用一次,或许让UIDocumentMenuViewController找不到presentingViewController
基础概念: 1、presentedViewController :The view controller that is presented by this view controller, or one of its ancestors in the view controller hierarchy. 2、presentingViewController: The view controller that presented this view controller (or its farthest ancestor.)
I 处理WebVC调摄像头之后,dismiss掉的问题
处理包括WebView的控制器调摄像头之后被UIDocumentMenuViewController干掉的问题
1.1 问题分析
UIDocumentMenuViewController 消失时调用的不仅调用了自己的,dismissViewControllerAnimated,还调用了,上层或许上上层presentingViewController的dismissViewControllerAnimated。 即会循环调用modal 办法的个个层级的dismissViewControllerAnimated,让一切的控制器消失。 因而当用户挑选takePhoto或许photoLibrary的时候,会导致被modal出来的webView 所在的控制器也会毁掉。
1.2 问题处理思路
只让UIDocumentMenuViewController 目标调用自己的dismissViewControllerAnimated,或许让UIDocumentMenuViewController找不到presentingViewController即可。
II 处理方案
2.1 方案1:只让UIDocumentMenuViewController 目标调用自己的dismissViewControllerAnimated办法
- 只让UIDocumentMenuViewController 目标调用自己的dismissViewControllerAnimated,不让加载H5 网页的控制器被刚掉(引荐使用此办法)
当时ViewController的一切presentedViewController都正常履行dismissViewControllerAnimated,当时ViewController自身履行dismissViewControllerAnimated,不进行dismiss,不做处理。
除非用户自己要求退出self.exitKNBaseWebViewControllerflagged = yes 或许 当想dismiss掉当时ViewController的时候,不能调用自身的dismissViewControllerAnimated ,直接调用父类的dismissViewControllerAnimated
重写拥有web View控制器的dismiss 办法
#if 1
-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
/**presentedViewController 本控制器即将present展现的控制器
思路,第一次 UIDocumentMenuViewController 展现摄影和photo界面的控制器 消失
第二次 self.presentedViewController nil 此刻不调用dismissViewControllerAnimated,, 假如自己要消失的话,需要新增个标识,表示是用户要回来
第三次 UIImagePickerController 相册、摄影控制器 消失
*/
if ( self.presentedViewController )
{
[super dismissViewControllerAnimated:flag completion:completion];
}
// if (self.exitKNBaseWebViewControllerflagged) {
// self.exitKNBaseWebViewControllerflagged = NO;
// [super dismissViewControllerAnimated:flag completion:completion];
// }
}
#endif
2.2 方案2:使UIDocumentMenuViewController找不到presentingViewController
处理办法就是不让UIDocumentMenuViewController找到上层或许上上层的恣意presentingViewController
/**
从控制器展现的办法presentViewController,处理
办法二,使UIDocumentMenuViewController找不到presentingViewController
*/
- (UIViewController *)presentingViewController {
// Avoiding iOS bug. UIWebView with file input doesn't work in modal view controller
if (_flagged) {
return nil;
} else {
return [super presentingViewController];
}
}
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
// Avoiding iOS bug. UIWebView with file input doesn't work in modal view controller
if ([viewControllerToPresent isKindOfClass:[UIDocumentMenuViewController class]]
||[viewControllerToPresent isKindOfClass:[UIImagePickerController class]]) {
_flagged = YES;
}
[super presentViewController:viewControllerToPresent animated:flag completion:completion];
}
2.3 demo 用法:
- 修改测试地址为你自己的URL
NSString *const PRODUCTIONBaseURL = @"https://kunnan.blog.csdn.net/article/details/103902362";
- 从CSDN下载demo https://download.csdn.net/download/u011018979/15133282
github.com/zhangkn/KNU…
see also
联络作者: iOS逆向(公号:iosrev)
作者简介:CSDN 博客专家认证丨全站 Top 50、华为如此享专家认证、iOS逆向公号号主
简历模板、技术合作。重视我,都给你。