从USB-C驱动器和iPad iOS13读取文件和内容

从USB-C驱动器和iPad iOS13读取文件和内容,usb,ios13,uidocumentpickerviewcontroller,Usb,Ios13,Uidocumentpickerviewcontroller,我创建了一个UIDocumentPickerViewController,并在USB驱动器上选择了我的文件夹,在使用枚举器列出文件后,如何读取内容 您能够读取文件的内容吗 以下是我的示例代码: @IBAction func openFiles(_ sender: Any) { // Using the Document Picker to Pick a Folder let documentPicker = UIDocumentPickerViewControl

我创建了一个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,我会收到读取错误


您有示例项目吗?

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

@IBAction func readFiles(){
//读取拾取的文件夹的内容
//pickedFolderURL必须是文件URL
let shouldStopAccess=pickedFolderURL.startAccessingSecurityScopedResource()
推迟{
如果应该停止访问{
pickedFolderURL.stopAccessingSecurityScopedResource()
}
}
var协调错误:N错误?
NSFileCoordinator().coordinate(读取项目地址:pickedFolderURL,错误:&coordinatedError){(folderURL)在中
let键:[URLResourceKey]=[.nameKey.isDirectoryKey]
让fileList=FileManager.default.enumerator(位于:pickedFolderURL,包括属性Forkeys:keys)!
对于案例,让文件作为文件列表中的URL{
guard pickedFolderURL.startAccessingSecurityScopedResource()其他{
fatalError(“在此处处理故障”)
返回;
}        
//确保完成后释放安全范围的资源。
延迟{pickedFolderURL.stopAccessingSecurityScopedResource()}
让newFile=file.path.replacingOccurrences(of:pickedFolderURL.path,带:“”)
如果(newFile.hasPrefix(“/”)==false){//exclude hidden
打印(文件)
}
}
self.logTextView.text=logString
}
}

我这里有一个几乎完全相同的问题——您必须使用securityScope和coordinator的组合。您是想再次使用
guard pickedFolderURL
还是
guard file
?不管是哪种方式--我尝试了对文件进行安全性范围界定,结果总是出现在
fatalError
部分范围界定文件夹将使您能够递归地访问整个子文件夹。。。我认为错误是elsewhere@Kal无论如何,我再次尝试了这段代码,它在没有权限的情况下运行。