我的应用程序能够导出和导入txt文件.我的.plist中的CFBundleDocumentTypes和UTExportedTypeDeclarations如下:
<key>CFBundleDocumentTypes</key> <array> <dict> <key>CFBundleTypeIconFiles</key> <array> <string>Icon60@2x</string> </array> <key>CFBundleTypeName</key> <string>CSV</string> <key>CFBundleTypeRole</key> <string>Default</string> <key>LSHandlerRank</key> <string>Default</string> <key>LSItemContentTypes</key> <array> <string>public.text</string> </array> </dict> </array> <key>UTExportedTypeDeclarations</key> <array> <dict> <key>UTTypeConformsTo</key> <array> <string>public.text</string> </array> <key>UTTypeDescription</key> <string>CSV</string> <key>UTTypeIdentifier</key> <string>public.text</string> <key>UTTypeTagSpecification</key> <dict> <key>public.filename-extension</key> <string>csv</string> <key>public.mime-type</key> <string>application/myApp</string> </dict> </dict> </array>
导出按预期工作,没有问题.导入也很有效,但只适用于存储在我的app iCloud文件夹之外的文件,即当我从邮件或通用iCloud文件夹中点击.txt文件时,我可以在“导入…”菜单旁边看到我的应用程序应用.但是,当我在iCloud上从我的app文件夹中点击完全相同的文件时,我的应用程序未列在“导入…”菜单中.
通过在iCloud上说我的app文件夹来澄清我的意思. App具有使用以下功能在iCloud上导出和保存某些数据的功能:
func autoExportToIcloudDrive(_ localFileURL:URL,fileNameST:String) { let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents").appendingPathComponent("\(fileNameST).csv"); if iCloudDocumentsURL != nil { let olderFileURL = iCloudDocumentsURL?.appendingPathComponent(fileNameST); if FileManager.default.fileExists(atPath: olderFileURL!.path) { self.uniFunc.deleteFileAtPath(pathUrl: olderFileURL!); } do { try FileManager.default.copyItem(at: localFileURL,to: iCloudDocumentsURL!); } catch { let nserror = error as NSError; fatalError("\(nserror),\(nserror.userInfo)"); } } }
这将在iCloud驱动器中创建app文件夹并将我的文件存储在那里.当我点击这个文件时,我无法在“导入…”菜单中看到我的应用程序.当我从app文件夹移出文件时,我的应用程序出现在“Import with …”菜单中.
这是一个预期的设计和应用程序不希望“看到”他们自己的iCloud驱动器文件夹中的文件或我在.plist文件中缺少某种额外的文件关联?
解决方法
这是预期的行为,因为“导入”与“打开”不同.通过导入文件,您可以将其内容作为应用数据的一部分.您的应用程序中的文件iCloud容器已经是(部分)您的应用程序数据,因此将它们导入您的应用程序数据是没有意义的.
UPDATE
CFBundleDocumentTypes下的以下属性将影响您的应用在文件上下文中的显示方式和方式:
> CFBundleTypeRole的值:“编辑器”与“查看器” – 持久编辑需要将文件导入应用程序数据(此处“默认”不是有效值)> Assigned LSHandlerRank:“Owner”,“Default”,“Alternate” – 声明是文件类型的主要应用程序(所有者)可能导致“Import with”部分被删除>注册正确的LSItemContentTypes:“.csv”文件将被识别为“public.comma-separated-values-text”(kUTTypeCommaSeparatedText)