2016-04-08 88 views
0

現在我正在開發應用程序,該應用程序應實現共享擴展以從郵件應用程序共享附件。它應該支持不同的文件擴展名(幾乎所有類型的文檔)。從Apple文檔中,我瞭解到我必須在Info.plist中使用Predicate,但在所有的答案中,我發現我必須在代碼中使用它。現在我陷入了困境,無法繼續前進。這是我想從此post使用的謂詞。從郵件應用程序分享附件在iOS中的分享擴展

SUBQUERY (
      extensionItems, 
      $extensionItem, 
      SUBQUERY (
      $extensionItem.attachments, 
      $attachment, 

      (
         ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.adobe.pdf" 
        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.image" 
        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.png" 
        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.jpeg" 
        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.jpeg-2000" 
        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.tiff" 
        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.compuserve.gif" 
        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.microsoft.bmp" 
        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.microsoft.word.doc" 
        || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "org.openxmlformats.wordprocessingml.document" 
      ) 
)[email protected] == [email protected] 
)[email protected] == 1 

誰能告訴我如何在我的SWIFT代碼中使用此斷言:

for attachment in content.attachments as! [NSItemProvider] { 
     if attachment.hasItemConformingToTypeIdentifier(contentType) { 

      attachment.loadItemForTypeIdentifier(contentType, options: nil) { data, error in 
       if error == nil { 
        let url = data as! NSURL 
        if let fileData = NSData(contentsOfURL: url) { 
         self.selectedFile = NSData(data: fileData) 
        } 
       } else { 

        let alert = UIAlertController(title: "Error", message: "Error loading file", preferredStyle: .Alert) 

        let action = UIAlertAction(title: "Error", style: .Cancel) { _ in 
         self.dismissViewControllerAnimated(true, completion: nil) 
        } 

        alert.addAction(action) 
        self.presentViewController(alert, animated: true, completion: nil) 
       } 
      } 
     } 
    } 

這裏是我的NSExtensionActivationRule:

 <key>NSExtensionActivationRule</key> 
     <dict> 
      <key>NSExtensionActivationSupportsAttachmentsWithMaxCount</key> 
      <integer>1</integer> 
     </dict> 

在此先感謝。

回答

2

所以最後我在我的問題上找到了答案!以防萬一有人會遇到同樣的問題。 首先,我必須在Info.plist中使用PREDICATE語句(子查詢),而不是NSExtensionActivationSupportsAttachmentsWithMaxCount。像:

 <key>NSExtensionActivationRule</key> 
     <string>SUBQUERY (
      extensionItems, 
      $extensionItem, 
      SUBQUERY (
      $extensionItem.attachments, 
      $attachment, 
      (
      ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.adobe.pdf" 
      || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.image" 
      || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.plain-text" 
      || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.png" 
      || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.jpeg" 
      || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.jpeg-2000" 
      || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.tiff" 
      || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.compuserve.gif" 
      || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.microsoft.bmp" 
      || ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "com.microsoft.word.doc" 
      ) 
      )[email protected] == 1 // Important! to activate extension only on 1 chosen image 
      )[email protected] == 1 
     </string> 

二:正確使用獲得必要TypeIdentifier(UTI)所有附件:

if let content = extensionContext!.inputItems.first as? NSExtensionItem { 
     if let contents = content.attachments as? [NSItemProvider] { 
      for attachment in contents{ 
       attachment.loadItemForTypeIdentifier("public.item", options: nil) { data, error in 
        let url = data as! NSURL 
        let fileExtension = url.pathExtension as String! 
        let fileName = self.generateImageName() as String 
        if let fileData = NSData(contentsOfURL: url) { 
         self.uploadFile("\(fileName).\(fileExtension)", data: fileData) 
        } 
       } 
      } 
     } 
    } 

「public.item」 - 是普遍的UTI支持所有類型的文件擴展名中列出您的NSExtensionActivationRule字符串。 你可以得到必要的UTI https://developer.apple.com

好運與發展的動作擴展!任何問題都歡迎!

+0

generateImageName()你能解釋一下這個函數做什麼嗎? –

+0

這是我的功能,它只是從拾取的文件的路徑中獲取原始文件名。 – Artiom

相關問題