title: Export UIDocument with custom file package UTI
body: <p>I'm trying to export my <code>UIDocument</code> subclass with a <code>UIDocumentPickerViewController</code>. The subclass writes data to a <code>FileWrapper</code> and its UTI conforms to <code>com.apple.package</code>.</p>
  
  <p>But the presented document picker shows <em>"Documents in iCloud Drive are not available because the iCloud Drive setting is disabled."</em></p>
  
  <p><img src="https://i.stack.imgur.com/Rcne1.png" width="250"></p>
  
  <p>The document is successfully written to the cache, as I can see from the exported container package.</p>
  
  <p><a href="https://i.stack.imgur.com/85fMN.png" rel="noreferrer"><img src="https://i.stack.imgur.com/85fMN.png" alt="Cache directory"></a></p>
  
  <p>When I change the document subclass and custom UTI to conform to a single file (e.g. <code>public.plain-text</code>), the document picker works fine and I can export the file. So the problem seems to be with the Document Type or Exported UTI.</p>
  
  <p>Am I doing something wrong or is this a bug?</p>
  
  <hr>
  
  <p><em>Info.plist</em></p>
  
  <p><a href="https://i.stack.imgur.com/nknxg.png" rel="noreferrer"><img src="https://i.stack.imgur.com/nknxg.png" alt="Document Type and Exported UTI"></a></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;key&gt;CFBundleDocumentTypes&lt;/key&gt;
  &lt;array&gt;
      &lt;dict&gt;
          &lt;key&gt;CFBundleTypeIconFiles&lt;/key&gt;
          &lt;array/&gt;
          &lt;key&gt;CFBundleTypeName&lt;/key&gt;
          &lt;string&gt;Custom Doc&lt;/string&gt;
          &lt;key&gt;LSHandlerRank&lt;/key&gt;
          &lt;string&gt;Owner&lt;/string&gt;
          &lt;key&gt;LSItemContentTypes&lt;/key&gt;
          &lt;array&gt;
              &lt;string&gt;com.zxzxlch.documentsandbox.customdoc&lt;/string&gt;
          &lt;/array&gt;
          &lt;key&gt;LSTypeIsPackage&lt;/key&gt;
          &lt;true/&gt;
      &lt;/dict&gt;
  &lt;/array&gt;
  
  ...
  
  &lt;key&gt;UTExportedTypeDeclarations&lt;/key&gt;
  &lt;array&gt;
      &lt;dict&gt;
          &lt;key&gt;UTTypeConformsTo&lt;/key&gt;
          &lt;array&gt;
              &lt;string&gt;com.apple.package&lt;/string&gt;
          &lt;/array&gt;
          &lt;key&gt;UTTypeDescription&lt;/key&gt;
          &lt;string&gt;Custom Doc File&lt;/string&gt;
          &lt;key&gt;UTTypeIdentifier&lt;/key&gt;
          &lt;string&gt;com.zxzxlch.documentsandbox.customdoc&lt;/string&gt;
          &lt;key&gt;UTTypeTagSpecification&lt;/key&gt;
          &lt;dict&gt;
              &lt;key&gt;public.filename-extension&lt;/key&gt;
              &lt;array&gt;
                  &lt;string&gt;zzz&lt;/string&gt;
              &lt;/array&gt;
          &lt;/dict&gt;
      &lt;/dict&gt;
  &lt;/array&gt;
  </code></pre>
  
  <p><em>CustomDocument.swift</em></p>
  
  
  
  <pre><code>private let textFilename = "contents.txt"
  
  class CustomDocument: UIDocument {
      var content = "Test"
  
      override func load(fromContents contents: Any, ofType typeName: String?) throws {
          guard let topFileWrapper = contents as? FileWrapper,
              let textData = topFileWrapper.fileWrappers?[textFilename]?.regularFileContents else {
                  return
          }
          content = String(data: textData, encoding: .utf8)!
      }
  
      override func contents(forType typeName: String) throws -&gt; Any {
          let textFileWrapper = FileWrapper(regularFileWithContents: content.data(using: .utf8)!)
          textFileWrapper.preferredFilename = textFilename
  
          return FileWrapper(directoryWithFileWrappers: [textFilename: textFileWrapper])
      }
  
  }
  </code></pre>
  
  <p><em>ViewController.swift</em></p>
  
  <pre><code>func exportDocument() {
      // Write to cache
      let cachesDir = FileManager.default.urls(for: FileManager.SearchPathDirectory.cachesDirectory, in: .allDomainsMask).first!
      let dataDir = cachesDir.appendingPathComponent("export", isDirectory: true)
      try! FileManager.default.createDirectory(at: dataDir, withIntermediateDirectories: true, attributes: nil)
  
      let fileURL = dataDir.appendingPathComponent("cookie").appendingPathExtension("zzz")
  
      let archive = CustomDocument(fileURL: fileURL)
      archive.content = "Cookie cat"
  
      archive.save(to: archive.fileURL, for: .forCreating) { success in
          guard success else {
              let alertController = UIAlertController.notice(title: "Cannot export data", message: nil)
              self.present(alertController, animated: true, completion: nil)
              return
          }
  
          let documentPicker = UIDocumentPickerViewController(url: archive.fileURL, in: .exportToService)
          documentPicker.delegate = self
          self.present(documentPicker, animated: true, completion: nil)
      }
  
  }
  </code></pre>