WKWebview的iCLoud文档选择器解除了容器视图

时间:2017-10-06 10:30:15

标签: ios swift wkwebview icloud-documents

我有一个WKWebview加载基于Web的UI,我希望用户能够从他们的iCloud文档上传文件。我已授予正确的权限,我可以浏览iCloud文档。但是,当我选择文件或单击取消按钮时,以及取消我的WKWebview的父视图的文档选择器视图也被取消。

我试图跟踪解雇路径。我100%肯定我没有在我的观点上调用解雇功能。

有没有人知道什么是触发我的WKWebview容器上的解雇以及如何防止它?

2 个答案:

答案 0 :(得分:3)

我在使用WKWebView的Objective-C和iOS11上遇到了同样的问题,并使用此解决方法解决了它。您应该可以轻松地将其迁移到Swift:

  • 我的WKWebView由一个直接扩展UIViewController的视图控制器拥有
  • 在此视图控制器中添加此弱属性

    @property (weak, nonatomic) UIDocumentPickerViewController *_Nullable docPickerPtr;

  • 在同一视图控制器中覆盖这两个方法,这两个方法最初是UIViewController基类的一部分

    - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion
    {
        if ([viewControllerToPresent isKindOfClass:[UIDocumentPickerViewController class]])
        {
            _docPickerPtr = (UIDocumentPickerViewController*)viewControllerToPresent;
        }
    
        [super presentViewController:viewControllerToPresent animated:flag completion:completion];
    }
    
    - (void)dismissViewControllerAnimated:(BOOL)flag
                               completion:(void (^)(void))completion
    {
        if (_docPickerPtr != nil && self.presentedViewController == nil)
        {
            NSLog(@">>>>>>>>>>>>PREVENT FROM DOING 2nd DISMISS!");
        }
        else
        {    
            [super dismissViewControllerAnimated:flag completion:completion];
        }
    }
    
  • 我们所做的是:

    1. 当我们要显示文档选择器时,保存一个指向UIDocumentPickerViewController的弱指针
    2. dismissViewControllerAnimated:completition被调用两次。一旦presentViewController不是nil还没有杀死实际的文档选择器,并且当presentViewController消失但UIDocumentPickerViewController仍然存在时第二次出于未知原因。这个想法是为了防止第二次解雇传播到超级

答案 1 :(得分:3)

UIDocumentPickerViewController中存在错误。

1)在视图控制器呈现UIDocumentPickerViewController的内部保存对UIDocumentPickerViewController的弱引用。 (这通常最终成为UINavigationController,因此您可能需要继承UINavigationController来解决此问题。)

///Due to a bug in UIDocumentPickerViewController we need to stop the UIDocumentPickerViewController from dismissing this navigation controller. Or at least provide control. This is a weak reference to a UIDocumentPickerController that this controller presents
weak var documentPicker: UIDocumentPickerViewController?

2)在呈现UIViewController

UIDocumentPickerViewController上覆盖这两个函数
//MARK: Overrides
override public func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
    if self.presentedViewController == nil && self.documentPicker != nil {
        self.documentPicker = nil
    }else{
        super.dismiss(animated: flag, completion: completion)
    }
}

public override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
    if viewControllerToPresent is UIDocumentPickerViewController {
        self.documentPicker = viewControllerToPresent as? UIDocumentPickerViewController
    }
    super.present(viewControllerToPresent, animated: flag, completion: completion)
}

现在来自UIDocumentPickerViewController的第二个电话不会驳回呈现UIViewController