iOS之Modal情况下使用WebView使用系统相册

来源:互联网 发布:ps软件在线升级 编辑:程序博客网 时间:2024/06/18 07:13

描述一下我今天遇到的bug情况:
1.app在Windows.rootViewController 下(rootViewController是一个NavigationController)push一个带WebView的Controller,WebView是打开网页有一个调用系统相册的页面,这个时候可以正常调用。
2.app在Windows.rootViewController之上通过presentViewController: animated: completion:加入一个NavigationController,这个NavigationController带一个WebView的Controller,webView上打开一个有调用系统相册的页面,这个时候调用系统相册出现

Warning: Attempt to present <UIImagePickerController: 0x1102c2a00> on <CustomNavigationController: 0x11020f600> whose view is not in the window hierarchy!

解决方法如下:
1.不使用presentViewController,全部用push
(显然这种方式是不现实的)
2.在你presentViewControoler要传入的控制器里重写

-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion{    if ( self.presentedViewController)    {        [super dismissViewControllerAnimated:flag completion:completion];    }}

如我上面的情况我重写了一个UINavigationController+Ext类别
UINavigationController+Ext.m代码如下:

#import "UINavigationController+Ext.h"@implementation UINavigationController (Ext)-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion{    if ( self.presentedViewController)    {        [super dismissViewControllerAnimated:flag completion:completion];    }}@end

这样就能很好的解决上面的问题了,但是最好不要使用类别,建议使用继承方式重写上面方法,然后app统一使用你新建的子类

参考资料:https://stackoverflow.com/questions/36457781/modal-uiwebview-and-camera-image-picker-issue

原创粉丝点击