2

I've got an App that implements all the bells and whistles of the FileProviderExtension. Now the question arises what is required to display download/upload progress in the Files app?

Out there is a single previously asked question with a rather vague answer which didn't yield any results.

This is the rough code of the startProvidingItem(at url: URL ...) function:

let downloadDelegate = DownloadDelegate(completionHandler, targetLocation: url, fileManager: fileManager)
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: downloadDelegate, delegateQueue: nil)
let downloadTask = session.downloadTask(with: sftpURL)

NSFileProviderManager.default.register(downloadTask, forItemWithIdentifier: id) { error in
    if let error = error {
         print("Error registering progress task \(error)")
    } else {
         print("Registered progress task")
         downloadTask.resume()
    }
}

Where the DownloadDelegate calls the completionHandler either with an error or nil on success. This all works but even though the downloadTask has been registered with the FileProviderManager no progress is shown.

It starts showing the indeterminate spinner and Waiting to Download below the file. The download task definitely sends progress events (confirmed with the urlSession(..., totalBytesWritten: ...) method in DownloadDelegate) but the FileProvider seemingly ignores them.


TL;DR: What is required to make download progress work in a FileExtension?

  • Did you managed to resolve the issue? I also have. I tried the suggested answer but it didn't help at all. I still see "waiting to download" until the download actually completes. – Gal Yedidovich Apr 20 '20 at 10:08
  • IIRC the accepted answer does work when using a regular URLSession. It didn't end up working for my use case though since I wasn't able to use .background on my customised URLSession (was using a custom protocol definition or smth). Make sure to set the documentSize property, otherwise it can't calculate the progress. – Til Blechschmidt Apr 20 '20 at 11:00
  • Thanks for the quick response, unfortunately it didn't work. I tried using *URLSession.shared* and also tried new session object with default configuration. The weird thing is, that with background config I can see progress from the preview page, But not in list view. I'm banging my head on this for over a week! – Gal Yedidovich Apr 20 '20 at 11:12
  • Could you clarify what you mean by "preview page"? The Files App preview of the downloading file? The thing with background downloads is that they are executed outside of your apps sandbox (thus the requirement for the shared container identifier). What list view are you referring to? – Til Blechschmidt Apr 20 '20 at 11:26
  • Easiest way is to show it: https://cti.ctera.com/invitations?share=dd8e8d1bc3331646c17b, link to video that shows the problem – Gal Yedidovich Apr 20 '20 at 11:33
  • My best guess would be that your URLSession is working perfectly and reports the progress and the fact that its not showing up in the file list is a bug with the Files App/UIDocumentBrowserVC. What happens when you start the download on a file from the list view, leave the folder (by e.g. pressing "MyProvider" in the top left) and then opening the folder again? My assumption is that the current folder just doesn't get the memo that the file download is in progress, I've had similar issues with plain iCloud downloads in the Files app lately. – Til Blechschmidt Apr 20 '20 at 11:44

1 Answers1

0

I have implemented and it's working fine. (See https://forums.developer.apple.com/thread/91280#292614)

  • The URLSession should use a .background configuration.
  • The configuration should have a valid sharedContainerIdentifier.
  • The container app of your extension should have the access to the shared container.

  • The extension should report the right file size via the documentSize property.

Jasmin
  • 794
  • 7
  • 18
  • You are actually right - turns out my issues were related to the download not using HTTP/HTTPS and thus the background session instantly terminated (somewhere in the documentation it states that background downloads are limited to those two protocols). – Til Blechschmidt Feb 15 '19 at 09:37
  • What would you consider a "valid sharedContainerIdentifier"? I'm facing the same issue – Gal Yedidovich Apr 19 '20 at 21:42
  • It refers the .sharedContainerIdentifier of the URLSessionConfiguration which determines the sharedContainer into which the files are downloaded/stored. [Apple Doc](https://developer.apple.com/documentation/foundation/urlsessionconfiguration/1409450-sharedcontaineridentifier) You have to set it to a valid container that you have specified in your Apps plist file. [Some more docs on that here](https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html), the download session basically behaves much like an app extension! – Til Blechschmidt Apr 20 '20 at 11:03