Hi,
I am trying to list the contents of a directory selected by the user using the following code:
This works for iCloud and for local files. It also, curiously, works on empty folders on the SMB server (though, obviously, it returns an empty array).
However, for SMB folders with contents I get the following cryptic error message.
In addition, modifying the code slightly so the user picks an image instead works just fine too. I can load the image from the supplied URL.
I have read the documentation here and watched the WWDC 2019 presentation, What's New in File Management and Quicklook, where this was introduced but this hasn't turned up any answers.
My app is set up with Supports Document Browser. Is there something I'm doing wrong? The way this is described what I'm trying to do, get a list of files and act on each of them, is exactly what this is intended for yet it's not working.
I am trying to list the contents of a directory selected by the user using the following code:
Code Block let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [.folder]) documentPicker.delegate = self self.present(documentPicker, animated: true)
Code Block func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { guard let folderUrl = urls.first else {return} let startAccessing = folderUrl.startAccessingSecurityScopedResource() guard startAccessing else { print("ERROR") return } defer { if startAccessing { folderUrl.stopAccessingSecurityScopedResource() } } var error : NSError? = nil NSFileCoordinator().coordinate(readingItemAt: folderUrl, error: &error) { (url) in let startAccessing = url.startAccessingSecurityScopedResource() guard startAccessing else { print("ERROR 2") return } defer { if startAccessing { folderUrl.stopAccessingSecurityScopedResource() } } do { let files = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: [.nameKey]) print(files) } catch let filesError { print(filesError) } } }
This works for iCloud and for local files. It also, curiously, works on empty folders on the SMB server (though, obviously, it returns an empty array).
However, for SMB folders with contents I get the following cryptic error message.
Code Block Error Domain=NSCocoaErrorDomain Code=256 "The file “downloads” couldn’t be opened." UserInfo={NSURL=file:///private/var/mobile/Library/LiveFiles/com.apple.filesystems.smbclientd/Gabgugfiles/downloads, NSFilePath=/private/var/mobile/Library/LiveFiles/com.apple.filesystems.smbclientd/Gabgugfiles/downloads, NSUnderlyingError=0x28010b0c0 {Error Domain=NSPOSIXErrorDomain Code=10006 "Unknown error: 10006"}}
In addition, modifying the code slightly so the user picks an image instead works just fine too. I can load the image from the supplied URL.
I have read the documentation here and watched the WWDC 2019 presentation, What's New in File Management and Quicklook, where this was introduced but this hasn't turned up any answers.
My app is set up with Supports Document Browser. Is there something I'm doing wrong? The way this is described what I'm trying to do, get a list of files and act on each of them, is exactly what this is intended for yet it's not working.