2017-01-25 31 views
1

我有一個共享的擴展,允許用戶將圖片添加到應用程序,斯威夫特,分享推廣:NSItemProvider loadItem不工作

everytings工作得很好,但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) 
} 
+0

是否引發錯誤? – Dopapp

+0

@Dopapp不,沒有任何反應 –

+0

@Dopapp你是什麼意思的isActive,如果你的意思是hasItemConformingToTypeIdentifier,是的,它返回true –

回答

0

廣場

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

在completionHandler結尾處爲attachment.loadItem()代替viewDidLoad()如下所示:

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

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