title: Thumbnail Metadata Disappeared When Importing File with Copy to App
body: <p>I have been using the URL Resources to embed the thumbnail metadata into my custom document-based file. When I export my custom document file, the thumbnail shows up nicely when glanced in iOS Files app.</p>
  
  <pre><code>override func fileAttributesToWrite(to url: URL, for saveOperation: UIDocumentSaveOperation) throws -&gt; [AnyHashable : Any] {
      var attr: [AnyHashable : Any] = [URLResourceKey.hasHiddenExtensionKey: true]
  
      // Ignore the proper resizing for now.
      ...
  
      attr[URLResourceKey.thumbnailDictionaryKey] = [
          URLThumbnailDictionaryItem.NSThumbnail1024x1024SizeKey: #imageLiteral(resourceName: "Star")
      ]
  
      return attr
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/FBWoW.png" rel="noreferrer"><img src="https://i.stack.imgur.com/FBWoW.png" alt="enter image description here"></a>
  (Icon Credit: <a href="https://useyourloaf.com" rel="noreferrer">Use Your Loaf</a>)</p>
  
  <p>However, when I import the file back into my app with Share action, <code>Copy to &lt;MyApp&gt;</code>, all the metadata seems to survive <strong>except the thumbnail</strong>.</p>
  
  <p><a href="https://i.stack.imgur.com/TUmlM.png" rel="noreferrer"><img src="https://i.stack.imgur.com/TUmlM.png" alt="enter image description here"></a></p>
  
  <pre><code>func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -&gt; Bool {
  
      // Other non-image metadata survives, like creationDate.
      if let creationDateOpt = try? url.resourceValues(forKeys: [.creationDateKey]).creationDate,
          let creationDate = creationDateOpt {
          print("creationDate: \(creationDate)")
      }
  
  
      if let thumbnailDictOpt = try? url.resourceValues(forKeys: [.thumbnailDictionaryKey]).thumbnailDictionary,
          let thumbnailDict = thumbnailDictOpt,
          let thumbnail = thumbnailDict[URLThumbnailDictionaryItem.NSThumbnail1024x1024SizeKey] {
          print ("Thumbnail survived. Size: \(thumbnail.size)")
          return true
      }
      print ("Thumbnail disappeard!")
      return false
  }
  </code></pre>
  
  <p>Considering that the thumbnail persists when I manual copy the file in Files app, the lost must happen during the system copying my file from Files to my app's container. Why does this happen? I can manually embed thumbnail as part of my file's data too, but I feel there should be a way to solve this, since other text-based metadata persists.</p>
  
  <p>I tried using <code>promisedResourceValue</code> method instead, suspecting that the file might not be fully copied when it's opened, but the result is the same.</p>
  
  <p><a href="https://i.stack.imgur.com/f2x07.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/f2x07.jpg" alt="enter image description here"></a></p>
  
  <p>I have my full project available <a href="https://github.com/pt2277/TestUIDocumentSave" rel="noreferrer">here on GitHub</a>.</p>