使用NSFileProviderExtension无法显示文件

时间:2017-10-28 03:36:36

标签: ios swift ios11 ios-extensions

我尝试实施NSFileProviderExtension,第一步,我只想尝试展示一件商品。

我在扩展程序中覆盖了此方法:

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
}

这个方法在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)
}

现在没有任何东西出现。 我还应该做些什么来只显示一个项目吗?

1 个答案:

答案 0 :(得分:1)

答案在这里: Items in File provider extension for ios 11

您必须实现 itemIdentifier,filename,typeIdentifier和parentItemIdentifier 属性才能显示FileProviderItem。它们是必需的,其他属性是可选的。

当用户在文件应用中打开您应用的文件提供商时,系统会调用枚举器(for:)方法,并传递 rootContainer 常量。因此,您必须为根级文件夹中的项目将 parentItemIdentifier 设置为 NSFileProviderItemIdentifier.rootContainer

如果您执行上述操作,系统会显示您的项目。