Trouble implementing FileProvider

Hello,


I try to implement a FileProvider for iOS11.


I succeed to list my items, but when I try to show a file by clicking on it, nothing happens.


I set my item with this parameter :


var isDownloaded: Bool {
        if let file = self.file {
            if FileManager().fileExists(atPath: Tools().getGenericFilePath(file: file).path) {
                return true
            } else {
                return false
            }
        }
        return false
    }


(If I don't set it, it shows me the placeholder)


It seems that :


providePlaceholder(at url: URL, completionHandler: ((_ error: Swift.Error?) -> Void)?)


is called multiple times (at least three).


Here is my enumerator :


override func enumerator(for containerItemIdentifier: NSFileProviderItemIdentifier) throws -> NSFileProviderEnumerator {
   
        let maybeEnumerator: NSFileProviderEnumerator? = nil
        if (containerItemIdentifier == NSFileProviderItemIdentifier.rootContainer) {
            return FileProviderEnumerator(enumeratedItemIdentifier: containerItemIdentifier)
        } else if (containerItemIdentifier == NSFileProviderItemIdentifier.workingSet) {
            // TODO and never called
        } else {
            if (containerItemIdentifier.rawValue.starts(with: "F-")) {
                // if my item is a folder
                return FileProviderEnumerator(enumeratedItemIdentifier: containerItemIdentifier)
            } else if (containerItemIdentifier.rawValue.starts(with: "f-")) {
               // if my item is a file
                self.startProvidingItem(at: self.urlForItem(withPersistentIdentifier: containerItemIdentifier)!, completionHandler: { error in
                    if let error = error {
                        print(error)
                    }
                })
                return FileProviderEnumerator(enumeratedItemIdentifier: containerItemIdentifier)
            }
        }
        guard let enumerator = maybeEnumerator else {
            throw NSError(domain: NSCocoaErrorDomain, code: NSFeatureUnsupportedError, userInfo:[:])
        }
        return enumerator
    }


Here is my startProvidingItem which is not called :


override func startProvidingItem(at url: URL, completionHandler: ((_ error: Swift.Error?) -> Void)?) {
     
        let pathComponents = url.pathComponents
     
        assert(pathComponents.count > 1)
        var explodedPath = pathComponents[pathComponents.count - 1].characters.split(separator: ".").map(String.init)
        if fileManager.fileExists(atPath: url.path) {
            completionHandler!(nil)
        } else {
            if let file = CoreDataStore.sharedInstance.getFileByHash(explodedPath[0], privateMoc: false) {
                let sessionConfig = URLSessionConfiguration.ephemeral
                let session = URLSession(configuration: sessionConfig)
                guard let request = FolderRouter.downloadFile(file.remoteID).URLRequest else {
                    return
                }
                let task = session.dataTask(with: request) { (data, response, error) -> Void in
                 
                    DispatchQueue.main.async {
                        if let data = data {
                            self.fileManager.createFile(atPath: url.absoluteString, contents: data, attributes: nil)
                            completionHandler!(nil)
                         
                        }
                    }
                }
                task.resume()
                NSFileProviderManager.default.register(task, forItemWithIdentifier: persistentIdentifierForItem(at: url)!, completionHandler: { error in
                    if let error = error {
                        print(error)
                    }
                })
             
            } else {
                completionHandler?(NSError(domain: NSCocoaErrorDomain, code: NSFeatureUnsupportedError, userInfo:[:]))
            }
        }
    }



Any idea what I'm missing or doing wrong ?

I wonder why apple is very not responsive in this area, and looks like many others are having difficulties implementing their helloworld for this feature.


Same confusion here. And there is no sample code anywhere on apple or google

when you click the item i.e file not folder then startProvidingItemAtURL is called if file is downloaded, in this method you should check if file exists in the given url or not it exists simply call complitionHandler(nil); otherwise download the file from the server and store it under the directory -> <itemidentifier>/<itemname> then call complitionHandler(nil).

Trouble implementing FileProvider
 
 
Q