There is a bug in UIDocumentPickerViewController
.
1) Save weak reference to UIDocumentPickerViewController
inside what ever view controller presents the UIDocumentPickerViewController
. (This usually end up being a UINavigationController
so you will probably have to subclass UINavigationController
to fix this.)
///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) Override these two functions on the UIViewController
that is presenting the 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)
}
Now the second call from the UIDocumentPickerViewController
will not dismiss the presenting UIViewController
.