Ios 在用户从文档选择器中选择pdf文件后,以编程方式更改其名称
我已经实现了文档选择器。用户只能选择pdf文件。当用户从文档选择器中选择pdf时,我得到该pdf文件的文件位置。前任。file://somepath/pdffile.pdf. 收到此文件位置URL后,我想将pdffile.pdf更改为newpdf.pdf并使用新创建的文件位置。您可以在将文件移动/复制到应用程序的本地存储时执行此操作 代码Ios 在用户从文档选择器中选择pdf文件后,以编程方式更改其名称,ios,objective-c,pdf,uidocumentpickerviewcontroller,Ios,Objective C,Pdf,Uidocumentpickerviewcontroller,我已经实现了文档选择器。用户只能选择pdf文件。当用户从文档选择器中选择pdf时,我得到该pdf文件的文件位置。前任。file://somepath/pdffile.pdf. 收到此文件位置URL后,我想将pdffile.pdf更改为newpdf.pdf并使用新创建的文件位置。您可以在将文件移动/复制到应用程序的本地存储时执行此操作 代码 /// When you finish picking up a file, you get it's current location in the del
/// 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
}
}