I am building a FileProviderExtension in Swift for my app. So far, I am able to enumerate files from a server, and attach thumbnails and metadata to display them in the browse tab of the Files app.
I would like to know what I need to do to have my FileProvider call startProvidingItem , so that the Files app can open my file in another application.
override func urlForItem(withPersistentIdentifier identifier: NSFileProviderItemIdentifier) -> URL? {
guard let item = try? item(for: identifier) else {
return nil
}
let manager = NSFileProviderManager.default
let perItemDirectory = manager.documentStorageURL.appendingPathComponent(identifier.rawValue, isDirectory: true)
return perItemDirectory.appendingPathComponent(item.filename, isDirectory:false)
}
override func providePlaceholder(at url: URL, completionHandler: @escaping (Error?) -> Void) {
guard let identifier = persistentIdentifierForItem(at: url) else {
completionHandler(NSFileProviderError(.noSuchItem))
return
}
do {
let fileProviderItem = try? item(for: identifier)
let placeholderURL = NSFileProviderManager.placeholderURL(for: url)
try NSFileProviderManager.writePlaceholder(at: placeholderURL,
withMetadata: fileProviderItem!)
completionHandler(nil)
}
catch let error {
completionHandler(error)
}
}When clicking on an item, urlForItem is called and returns a url to a file in the AppGroup directory of the iPhone simulator:
file:///Users/.../Library/Developer/CoreSimulator/Devices/508AD191-5B30-48F9-B28C-0CA1657AC385/data/Containers/Shared/AppGroup/354B4772-9441-435B-B1C9-21C3E072335F/File%20Provider%20Storage/testItemId_1/file_1.pngI would expect that startProvidingItem gets called next, but instead, providePlaceholder is called with that url and catches the following error (line 25):
Error Domain=NSCocoaErrorDomain Code=4 "The folder “.file_1.png.icloud” doesn’t exist." UserInfo={NSFilePath=/Users/.../Library/Developer/CoreSimulator/Devices/508AD191-5B30-48F9-B28C-0CA1657AC385/data/Containers/Shared/AppGroup/354B4772-9441-435B-B1C9-21C3E072335F/File Provider Storage/testItemId_1/.file_1.png.icloud, NSUserStringVariant=Folder, NSUnderlyingError=0x7fa6d91adbb0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}Why is the file error message indicating a folder and the weird .icloud file name? When I look in the File Provider Storage directory, it is empty. Do I need to copy the file there? If so, where in my extension is the right step to do this?