ios - 用户从文档选择器中选择 pdf 文件后,以编程方式更改其名称
问题描述
我已经实现了文档选择器。用户只能选择 pdf 文件。当用户从 Document Picker 中选择 pdf 时,我会得到该 pdf 文件的文件位置。前任。文件://somepath/pdffile.pdf。收到此文件位置 URL 后,我想将 pdffile.pdf 更改为 newpdf.pdf 并使用该新创建的文件位置。
解决方案
您可以在将文件移动/复制到应用程序的本地存储时执行此操作。
代码
/// When you finish picking up a file, you get it's current location in the delegate callback like this.
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
/// Assumption is that you are picking only one file at a time.
guard let url = urls.first else { return }
do {
/// You can copy this move this file to your Documents directory
let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let newFileName = "custom_name.pdf"
let newFilePath = "\(documentsDirectory)/\(newFileName)"
try FileManager.default.moveItem(at: url, to: URL(fileURLWithPath: newFilePath))
} catch {
/// Handle error
}
}
推荐阅读
- pine-script - 仅在特定烛台上执行命令
- swift - 在guard let url之后,值仍然需要被解包
- python - CSV:如何从列表列表(包含列表的列表)中找到最接近的匹配/最接近的值?
- php - 根据 laravel-8 中的 product_id 显示所有数据
- sql - 获取 BigQuery 中每个键的下一个值(如果存在)
- javascript - 如何在 videojs 上添加质量选择器选项
- flutter - 无法使用 Flutter Mobile 上传到 Firebase 存储
- r - R查找多个数据框的匹配列名
- python - 无法在 django 中保存散列密码
- flutter - placemarkFromCoordinates() 在颤振 goecoding 插件中不起作用