首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift,共享扩展: NSItemProvider loadItem不工作

Swift,共享扩展: NSItemProvider loadItem不工作
EN

Stack Overflow用户
提问于 2017-01-26 00:22:03
回答 1查看 1.1K关注 0票数 2

我有一个分享扩展,允许用户添加图片到应用程序,

一切都很好,但NSItemProvider .loadItem不工作

这是我在SLComposeServiceViewController中的代码:

代码语言:javascript
复制
override func viewDidLoad() {

    let content = extensionContext!.inputItems[0] as! NSExtensionItem

    for attachment in content.attachments as! [NSItemProvider] {

        let identifier = kUTTypeJPEG as String
        let hasItemConforming =  attachment.hasItemConformingToTypeIdentifier(identifier)

        print("LOG : \(hasItemConforming)") // print True ! so item has conforming

        if hasItemConforming {

            attachment.loadItem(forTypeIdentifier: identifier , options: nil, completionHandler: { (coding:NSSecureCoding?, error:Error!) in

                // this func not works and prints nothing

                print("LOG : Loaded") // print nothing
                print("LOG : error : \(error)") // print nothing
                print("LOG : secureCoding : \(coding == nil)") // print nothing

            })
        }

    }

    self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
}
EN

回答 1

Stack Overflow用户

发布于 2018-02-23 18:09:13

地点

代码语言:javascript
复制
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil) 

在attachment.loadItem()而不是viewDidLoad()的completionHandler末尾,如下所示:

代码语言:javascript
复制
attachment.loadItem(forTypeIdentifier: identifier , options: nil, completionHandler: {
    data, error in
    // Your code here

    self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil) 
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41856578

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档