Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在用户从文档选择器中选择pdf文件后,以编程方式更改其名称_Ios_Objective C_Pdf_Uidocumentpickerviewcontroller - Fatal编程技术网

Ios 在用户从文档选择器中选择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

我已经实现了文档选择器。用户只能选择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 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
    }
}