From 8bd6fe1e117fad5e1bf8670b7be58130741f6a6d Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Mon, 19 Jun 2023 19:05:27 -0500 Subject: [PATCH] Replaces UIDocumentBrowserViewController with UIDocumentPickerViewController on iOS 17+ Prior to iOS 17, UIDocumentPickerViewController was too buggy to reliably use with iCloud Drive. --- Delta/Importing/ImportController.swift | 56 +++++++++++++++++++++----- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/Delta/Importing/ImportController.swift b/Delta/Importing/ImportController.swift index 7c40864..4b506fb 100644 --- a/Delta/Importing/ImportController.swift +++ b/Delta/Importing/ImportController.swift @@ -8,6 +8,7 @@ import UIKit import MobileCoreServices +import UniformTypeIdentifiers import ObjectiveC import DeltaCore @@ -147,18 +148,38 @@ class ImportController: NSObject private func presentDocumentBrowser() { - let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(ImportController.cancel)) + let supportedTypes = self.documentTypes.compactMap { UTType($0) } - let documentBrowserViewController = UIDocumentBrowserViewController(forOpeningFilesWithContentTypes: Array(self.documentTypes)) - documentBrowserViewController.delegate = self - documentBrowserViewController.modalPresentationStyle = .fullScreen - documentBrowserViewController.browserUserInterfaceStyle = .dark - documentBrowserViewController.allowsPickingMultipleItems = true - documentBrowserViewController.allowsDocumentCreation = false - documentBrowserViewController.additionalTrailingNavigationBarButtonItems = [cancelButton] + let presentedViewController: UIViewController - self.presentedViewController = documentBrowserViewController - self.presentingViewController?.present(documentBrowserViewController, animated: true, completion: nil) + if #available(iOS 17, *) + { + // Prior to iOS 17, UIDocumentPickerViewController was too buggy to reliably use with iCloud Drive. + + let documentPickerViewController = UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes, asCopy: true) + documentPickerViewController.delegate = self + documentPickerViewController.overrideUserInterfaceStyle = .dark + documentPickerViewController.allowsMultipleSelection = true + + presentedViewController = documentPickerViewController + } + else + { + let documentBrowserViewController = UIDocumentBrowserViewController(forOpening: supportedTypes) + documentBrowserViewController.delegate = self + documentBrowserViewController.modalPresentationStyle = .fullScreen + documentBrowserViewController.browserUserInterfaceStyle = .dark + documentBrowserViewController.allowsPickingMultipleItems = true + documentBrowserViewController.allowsDocumentCreation = false + + let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(ImportController.cancel)) + documentBrowserViewController.additionalTrailingNavigationBarButtonItems = [cancelButton] + + presentedViewController = documentBrowserViewController + } + + self.presentedViewController = presentedViewController + self.presentingViewController?.present(presentedViewController, animated: true, completion: nil) } } @@ -195,9 +216,22 @@ extension ImportController } } +extension ImportController: UIDocumentPickerDelegate +{ + func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt documentURLs: [URL]) + { + self.finish(with: Set(documentURLs), errors: []) + } + + func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) + { + self.cancel() + } +} + extension ImportController: UIDocumentBrowserViewControllerDelegate { - func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentURLs documentURLs: [URL]) + func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentsAt documentURLs: [URL]) { var coordinatedURLs = Set() var errors = [Error]()