Search code examples
iosswiftnskeyedarchiveruidocumentpickerviewcontroller

How to restrict UIDocumentPickerViewController import to .archive files? (NSKeyedArchiver)


In an app that saves data as an archive (NSKeyedArchiver), I am allowing users to export the .archive file to have a backup. Users can then re-import those files to retrieve backed up data.

Is there a way to restrict UIDocumentPickerViewController to only allow import of those .archive files? I tried using kUTTypeArchive as the only allowed type, but it doesn't appear to be the same kind of "archive", so I've used "public.item" while I look for a solution.

Running the mdls command on the file gives this file type: dyn.ah62d4rv4ge80c6xdrby1q3k

PS: If exporting and importing .archive files sounds like bad practice, I'm very interested to hear why!


Solution

  • The ".archive" file extension you use is not standard, it's an arbitrary file extension that you chose.

    So your app should define its own custom file extension and UTI to represent your archive files. The fact that the file is created from using NSKeyedArchiver is irrelevant to the user.

    Define your custom UTI and file extension under the Exported UTIs section. Then add it to the Document Types section as well.

    Then use your custom UTI with UIDocumentPickerViewController so only files from your app with your app's custom extension are selectable.