作者:虎爷在江湖 | 来源:互联网 | 2022-12-12 11:58
1> Paulw11..:
他们似乎希望您使用它UIDocumentPickerViewController
来使用户能够根据第2.5.15条从云服务以及照片库中选择视频文件
Apple希望他们的客户能够很好地体验他们的设备及其运行的应用程序,因此您的应用程序支持所有相关的iOS功能是有意义的.
您可以使用以下命令创建节目文档选择器以选择视频文件:
let picker = UIDocumentPickerViewController(documentTypes: ["public.movie"], in: .import)
picker.delegate = self
self.show(picker, sender: self)
您需要实现一些委托代码来处理所选文档.例如,要将所选文件复制到应用程序的文档目录中:
extension ViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
if let pickedUrl = urls.first {
let filename = pickedUrl.lastPathComponent
self.filename.text = filename
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
var documentsDirectory = paths[0]
// Apend filename (name+extension) to URL
documentsDirectory.appendPathComponent(filename)
do {
// If file with same name exists remove it (replace file with new one)
if FileManager.default.fileExists(atPath: documentsDirectory.path) {
try FileManager.default.removeItem(atPath: documentsDirectory.path)
}
// Move file from app_id-Inbox to tmp/filename
try FileManager.default.moveItem(atPath: pickedUrl.path, toPath: documentsDirectory.path)
UserDefaults.standard.set(filename, forKey:"filename")
UserDefaults.standard.set(documentsDirectory, forKey:"fileurl")
self.fileURL = documentsDirectory
} catch {
print(error.localizedDescription)
}
}
}
}