如何在iOS共享扩展中获取共享对象的文件名

coy*_*yer 3 ios

基本上我想从其他应用程序(例如dropbox)检索文件(例如pdf)以便稍后存储和修改.

我为该任务编写了一个共享扩展,可以迭代NSExtensionItem并可以找出我的文件 - 但我不知道他们的原始文件名.

我注意到其他应用程序获得了文件名 - 但是在iOS中使用"打开方式"功能调用它们.

那么我如何在我的共享扩展中获取文件名?

提前致谢.

小智 6

您在收到的URL中将文件名作为lastPathComponent:

    itemProvider!.first!.loadItemForTypeIdentifier(kUTTypeImage as String, options: nil, completionHandler: {
        (provider, error) in
        let url = provider as! NSURL

        println(url)

        let data = NSData(contentsOfURL: url)

        let name = url.lastPathComponent!

        ...

        // Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
        self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
    })
Run Code Online (Sandbox Code Playgroud)