1

I'm trying to implement NSFileProviderExtension, for the first step, I'm just trying to display a single item.

I override this method in my extension:

override func enumerator(for containerItemIdentifier: NSFileProviderItemIdentifier) throws -> NSFileProviderEnumerator {
    var maybeEnumerator: NSFileProviderEnumerator? = nil
    if (containerItemIdentifier == NSFileProviderItemIdentifier.rootContainer) {
        // TODO: instantiate an enumerator for the container root
        print("Root Container")
        maybeEnumerator = FileProviderEnumerator(enumeratedItemIdentifier: NSFileProviderItemIdentifier(rawValue: "rootContainer"))
    } else if (containerItemIdentifier == NSFileProviderItemIdentifier.workingSet) {
        // TODO: instantiate an enumerator for the working set
        print("Working Set")
    } else {
        // TODO: determine if the item is a directory or a file
        // - for a directory, instantiate an enumerator of its subitems
        // - for a file, instantiate an enumerator that observes changes to the file
        print("Other Stuff")
    }
    guard let enumerator = maybeEnumerator else {
        print("Enumerator Is nil, Throwing Error")
        throw NSError(domain: NSCocoaErrorDomain, code: NSFeatureUnsupportedError, userInfo:[:])
    }
    return enumerator
}

And this method in FileProviderEnumerator:

    func enumerateItems(for observer: NSFileProviderEnumerationObserver, startingAt page: NSFileProviderPage) {
    /* TODO:
     - inspect the page to determine whether this is an initial or a follow-up request

     If this is an enumerator for a directory, the root container or all directories:
     - perform a server request to fetch directory contents
     If this is an enumerator for the active set:
     - perform a server request to update your local database
     - fetch the active set from your local database

     - inform the observer about the items returned by the server (possibly multiple times)
     - inform the observer that you are finished with this page
     */


    let items = [FileProviderItem(id: "1", name: "1_file")]
    observer.didEnumerate(items)
    print("Finish Enumerating")
    observer.finishEnumerating(upTo: nil)
}

Nothing shows up right now. Is there anything else I should do to display only a single item?

clemens
  • 16,716
  • 11
  • 50
  • 65

1 Answers1

1

The answer is here: Items in File provider extension for ios 11

You have to implement itemIdentifier, filename, typeIdentifier and parentItemIdentifier properties to show FileProviderItem. They are required, other properties are optional.

When an user open your app's file provider in files app, the system calls enumerator(for:) method and it passes the rootContainer constant. So, you must to set parentItemIdentifier to NSFileProviderItemIdentifier.rootContainer for your item in root-level folder.

If you implement above, your item will shown.

Developer
  • 51
  • 7
  • 1
    Any idea why my folders look like files? I set `typeIdentifier` to `(NSString*)kUTTypeDirectory` but they still look like plain files and tapping them tries to preview the file instead of navigating inside. The Info panel does say they are directories. – Rivera Aug 22 '18 at 08:01