I am trying to upload a ZIP file, created by compressing other files in iCloud Drive through the Files app, using UIDocumentPicker. However, errors like those described in Test 1 and Test 2 in the sample code are occurring.
import UIKit
class ViewController: UIViewController,UIDocumentPickerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func openDocumentPicker(_ sender: Any) {
// Test 1
// let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [.item], asCopy: true)
// When clicking the "Open" button, there is no response, and the following error occurs.
/*
Failed to copy the imported file into the local container ((null))
Tried to call delegate -documentBrowser:didPickDocumentURLs: with an empty array of items. This indicates the items failed to be prepared and materialized on disk: (
"<DOCItemBookmark: 0x303ad2fc0> FPItem=(null)"
)
*/
// Test 2
let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [.item])
// When using the regular mode instead of the copy mode,
// the "Open" button works, and the selectedFileURL is correctly retrieved.
// However, even after using startAccessingSecurityScopedResource,
// attempting to read the file results in the following error.
/*
Error reading file: Error Domain=NSCocoaErrorDomain Code=257 "The file “archive 4.zip” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/samplefiles/archive 4.zip, NSURL=file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/%E1%84%89%E1%85%A2%E1%86%B7%E1%84%91%E1%85%B3%E1%86%AF%E1%84%91%E1%85%A1%E1%84%8B%E1%85%B5%E1%86%AF/%E1%84%8B%E1%85%A1%E1%84%8F%E1%85%A1%E1%84%8B%E1%85%B5%E1%84%87%E1%85%B3%204.zip, NSUnderlyingError=0x300bedbf0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}
*/
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = true
self.present(documentPicker, animated: true, completion: nil)
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard urls.isEmpty == false else {
print("no urls")
return
}
//Test2
for selectedFileURL in urls {
// Start accessing the security-scoped resource
if selectedFileURL.startAccessingSecurityScopedResource() {
defer { selectedFileURL.stopAccessingSecurityScopedResource() }
// Perform operations on the file
do {
let fileCoordinator = NSFileCoordinator()
var error: NSError?
fileCoordinator.coordinate(readingItemAt: selectedFileURL, options: [], error: &error) { (newURL) in
do {
let fileData = try Data(contentsOf: newURL)
// Process the file data
print("File data read successfully")
} catch {
print("Error reading file: \(error)")
}
}
if let error = error {
print("Error coordinating file access: \(error)")
}
} catch {
print("Error reading file: \(error)")
}
} else {
print("Failed to access security-scoped resource")
}
}
}
It seems that there might be an issue with the compressed ZIP file. How can this be resolved?