iOS 小技能:app嵌入H5页面的通用模版

2022-08-22 13:03:54 浏览数 (1)

前言

Avoiding iOS bug:UIWebView with file input doesn‘t work in modal view controller

  • 从CSDN下载demo https://download.csdn.net/download/u011018979/15133282

https://github.com/zhangkn/KNUIWebViewWithFileInput.git

  • 应用场景: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 方法

代码语言:javascript复制
#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

代码语言:javascript复制

/**
 从控制器展示的方法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
代码语言:javascript复制
NSString *const PRODUCTIONBaseURL = @"https://kunnan.blog.csdn.net/article/details/103902362";
  • 从CSDN下载demo https://download.csdn.net/download/u011018979/15133282

https://github.com/zhangkn/KNUIWebViewWithFileInput.git

0 人点赞