iOS 15.0 - App Document Folder not showing downloaded files on Files App

I am trying to create an app that downloads files and stores these files in the document folder that can be seen inside the Files App.

I added these to the Info.plist file

	<key>UIFileSharingEnabled</key>
	<true/>
	<key>LSSupportsOpeningDocumentsInPlace</key>
	<true/>

I also added debugging code to test that the file is indeed inside the document folder. The code can see the file, but the Files app doesn't show my app document's folder by itself (not even after adding files manually). There is a special case that does show my app folder inside the Files app: when I move an existing file I have to my app document folder. That is the only way that the Files app shows my app document folder - but the strange part is that still it doesn't show the file I downloaded inside (only the file I moved manually).

Also: I tested on the simulator using iPhone 12 Pro and on a real device: same result. I deleted and re-installed the app many times, and also restarted Xcode and restarted the real device - nothing changed.

This is the code that downloads the file into my app document folder.

func downloadFile(urlString: String, filename: String, completionHandler: @escaping (_ err: Error?) -> Void) {
    let config = URLSessionConfiguration.default
     
    guard let url = URL(string: urlString), let documentPathURL = getDestFileURL() else { return }
     
    let request = URLRequest(url: url)
    let session = URLSession(configuration: config)
     
    os_log("getting download folder %{public}@", documentPathURL.path as CVarArg)
    let fileManager = FileManager()
    let task = session.downloadTask(with: request) { url, response, error in
      if error != nil {
        os_log("error: %{public}@", error! as CVarArg)
        completionHandler(error)
        return
      }

      guard let fileURL = url else { return }
      let fileNameParts = filename.components(separatedBy: ".")

       do {
        var isDir:ObjCBool = true
        // download folder exists?
        if !FileManager.default.fileExists(atPath: documentPathURL.path, isDirectory: &isDir) {
          os_log("creating new folder")
          try FileManager.default.createDirectory(atPath: documentPathURL.path, withIntermediateDirectories: true, attributes: nil)
        }
        let savePathURL = documentPathURL.appendingPathComponent(fileNameParts[0]).appendingPathExtension(fileNameParts[1])
        // dest file exists?
        if FileManager.default.fileExists(atPath: savePathURL.path) {
          os_log("removing existing file")
          try FileManager.default.removeItem(atPath: savePathURL.path)
        }
        // all good? then move the file!
        try fileManager.moveItem(at: fileURL, to: savePathURL)
        os_log("from path %{public}@", fileURL.path)
        os_log("to path: %{public}@", savePathURL.path)
        // dest file exists?
        if FileManager.default.fileExists(atPath: savePathURL.path) {
          let files = try fileManager.contentsOfDirectory(atPath: documentPathURL.path)
          try fileManager.setAttributes([FileAttributeKey.protectionKey : FileProtectionType.none, FileAttributeKey.posixPermissions: 0o777], ofItemAtPath: savePathURL.path)
          let attrs = try fileManager.attributesOfItem(atPath: savePathURL.path)
          os_log("list of files %{public}@", files as CVarArg)
          os_log("attrs of file %{public}@", attrs as CVarArg)
          os_log("move was a success")
        }
         
        completionHandler(nil)
      }
      catch {
        os_log("final error: %{public}@", error as CVarArg)
        completionHandler(MyError.couldNotDownload)
      }
  }

Can anyone see something wrong or missing? Thanks

iOS 15.0 - App Document Folder not showing downloaded files on Files App
 
 
Q