1

我创建了一个 UIDocumentPickerViewController 并在 USB 驱动器上选择了我的文件夹,在使用枚举器列出文件后,我如何读取内容?

你能读取文件的内容吗?

这是我的示例代码:

@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  
        }  
    }  

现在我想阅读 txt/mp4 文件的内容......怎么样?使用 quicklook 或 AVPlayerViewController 我得到阅读错误......

你有示例项目吗?

4

1 回答 1

1

解决方案是结合使用 securityScope 和文件 NSFileCoordinator

@IBAction func readFiles(){
        // Reading the Content of a Picked Folder
 // pickedFolderURL must be a fileURL
        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)!
            for case let file as URL in fileList {

                guard pickedFolderURL.startAccessingSecurityScopedResource() else {
                    fatalError("Handle the failure here.")
                    return;
                }        
                // Make sure you release the security-scoped resource when you are done.
                defer { pickedFolderURL.stopAccessingSecurityScopedResource() }

                let newFile = file.path.replacingOccurrences(of: pickedFolderURL.path, with: "")
                if(newFile.hasPrefix("/.") == false){ //exclude hidden
                    print(file)
                }
            }
            self.logTextView.text = logString
        }
    }
于 2020-03-03T15:06:25.520 回答