I created a UIDocumentPickerViewController and picked my folder on USB drive, after listing files with an enumerator, how can i read the contents?
Are you able to read the contents of a file?
Here is my sample code:
@IBAction func openFiles(_ sender: Any) {
// Using the Document Picker to Pick a Folder
let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypeFolder as String], in: .open)
documentPicker.delegate = self
documentPicker.shouldShowFileExtensions = true
present(documentPicker, animated: true, completion: nil)
}
@IBAction func readFiles(){
// Reading the Content of a Picked Folder
let shouldStopAccessing = pickedFolderURL.startAccessingSecurityScopedResource()
defer {
if shouldStopAccessing {
pickedFolderURL.stopAccessingSecurityScopedResource()
}
}
var coordinatedError:NSError?
NSFileCoordinator().coordinate(readingItemAt: pickedFolderURL, error: &coordinatedError) { (folderURL) in
let keys : [URLResourceKey] = [.nameKey, .isDirectoryKey]
let fileList = FileManager.default.enumerator(at: pickedFolderURL, includingPropertiesForKeys: keys)!
logString = ""
for case let file as URL in fileList {
let newFile = file.path.replacingOccurrences(of: pickedFolderURL.path, with: "")
if(newFile.hasPrefix("/.") == false){ //exclude hidden
print(file)
logString += "\n\(file)"
if (file.pathExtension == "mp4"){
self.pickedVideoURL = file
}
if (file.pathExtension == "txt"){
self.pickedTextURL = file
}
}
}
self.logTextView.text = logString
}
}
now i want to read the contents of the txt/mp4 file... how? Using quicklook or AVPlayerViewController I get reading errors...
Have you sample project?