3

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

我试图跟踪解雇路径。我 100% 确定我没有在我的视图中调用关闭函数。

有谁知道是什么触发了我的 WKWebview 容器的关闭以及如何防止它?

4

2 回答 2

4

中有一个错误UIDocumentPickerViewController

UIDocumentPickerViewController1)保存对视图控制器呈现的内部的弱引用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)
}

现在来自 the 的第二个电话UIDocumentPickerViewController不会关闭呈现UIViewController

于 2018-02-05T22:07:09.463 回答
3

I had the same problem on Objective-C and iOS11 with WKWebView and solved it using this workaround. You should be able to migrate it to Swift easily :

  • my WKWebView was owned by a view controller directly extending UIViewController
  • inside this view controller add this weak property

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

  • inside same view controller override these two methods originally part of the UIViewController base class

    - (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];
        }
    }
    
  • what we do is :

    1. when we're about to display the document picker, save a weak pointer to the UIDocumentPickerViewController
    2. the dismissViewControllerAnimated:completition gets called twice. Once while the presentedViewController is not nil yet to kill the actual document picker, and a 2nd time for unknown reasons when presentedViewController is gone but UIDocumentPickerViewController is still alive. The idea is to prevent this 2nd dismiss propagate to super
于 2017-11-06T22:56:32.740 回答