Swift,Share Extension:NSItemProvider loadItem无效

时间:2017-01-25 16:22:03

标签: ios swift xcode

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

每个人的工作都很好,但NSItemProvider .loadItem无法正常工作

这是我在SLComposeServiceViewController中的代码:

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)
}

1 个答案:

答案 0 :(得分:1)

放置

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

在completion.loadItem()的completionHandler结束时代替viewDidLoad(),如下所示:

attachment.loadItem(forTypeIdentifier: identifier , options: nil, completionHandler: {
    data, error in
    // Your code here

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