使用元数据将应用程序对象链接到磁盘上的文件

问题描述

遵循此主题iOS PDFkit cannot add custom attribute to pdf document

我的应用程序正在使用PDFKit保存文件 我正在尝试将自定义键元数据设置为保存在设备上的PDFDocument。 我的应用程序中的对象(“测试”)具有两个重要属性

这会引起一些问题:

  1. 文件名是不可读的UUID,因此用户体验很差。
  2. 如果用户重命名文件,则该应用将无法获取文件

因此,标识应具有文件的可读标签。因此,当用户打开File应用程序时,他可以轻松找到它。并添加带有ID的元数据,以便我的应用即使重命名也可以检索它。看起来是一个不错的解决方案,对吗?

// First I create my own attribute
fileprivate extension PDFDocumentAttribute {
    static let testId = PDFDocumentAttribute(rawValue: "com.sc.testID")
}

// Then I set the news attributes before saving the file,in the 'test' class
func setDocument(pdf: PDFDocument) {
        let fileURL = self.getPDFDocumentURL()
        print("the Metadata is \(pdf.documentAttributes)") // print an empty dictionary 
        pdf.documentAttributes?[PDFDocumentAttribute.testId] = self.id
        pdf.documentAttributes?[PDFDocumentAttribute.titleAttribute] = self.name // I suppose the ddisplay name of the document ? It's not,so what is that ?
        print("the Metadata is Now \(pdf.documentAttributes)") // both are printed,it looks ok

        //pdf.write(to: fileURL) // I tested this one too,same issues
        
        let data = pdf.dataRepresentation()
        do {
            try data?.write(to: fileURL,options: .completeFileProtection)
        } catch {
            print(error.localizedDescription)
        }
    }

从这里看起来还不错,当我想检索pdf文档时,我将在文件夹中检查每个文档的ID,并在ID匹配时返回该文档。但是问题是,当我获得documentAttributes属性时,“ testId”属性不在其中。请注意,本机标题设置正确。 这样我就可以从那里获取ID了,但这似乎是不合适的

//still in 'Test' class
 func getPDFDocument() -> PDFDocument? {
        // get all docs in the folder ad check Metadata for each
        let fileManager = FileManager.default
        let documentsURL = fileManager.urls(for: .documentDirectory,in: .userDomainMask)[0]
        do {
            let fileURLs = try fileManager.contentsOfDirectory(at: SchoolTest.getSubjectFolderURL(subject: self.subject!),includingPropertiesForKeys: nil)
            for url in fileURLs {
                print("the doc attributes are : \(PDFDocument(url: url)?.documentAttributes)") // contain title and others preset by Apple but not my custom 'testId'
                if let doc = PDFDocument(url: url),doc.documentAttributes?[PDFDocumentAttribute.titleAttribute/*testId*/] as? String == self.documentName {
                    return doc // this work temporary
                }
            }
        } catch {
            print("Error while enumerating files \(documentsURL.path): \(error.localizedDescription)")
        }
        return nil
    }

显示名称 当前,“文件”应用中显示显示名称/标签文件名称(来自URL)。 这也可能引起问题,因为如果两个“测试”具有相同的名称,则它们的链接文件将具有相同的URL。因此,当最新的一个保存在磁盘上时,它将覆盖另一个。 当使用'Test'id属性作为文件URL时,我没有这个问题。 如果我可以为文件设置显示名称,并使用可以解决问题的UUID保留URL。 目录有相同的本地化问题,我用英语命名它们,但Apple本机目录已本地化。本地化名称可能对用户体验很有帮助。 经过数小时的研究,我找不到本地化或将显示名称应用于文件/目录的方法

// I tried that without positive result
var url = fileURL as NSURL
try url.setResourceValue("localized label",forKey: .localizedLabelKey)
print("localized name is \(try url.resourceValues(forKeys: [.localizedLabelKey]))")
let newURL = url as URL
try data?.write(to: newURL,options: .completeFileProtection)

我做得不好吗?将自定义元数据添加文件中时应该怎么做?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)