2016-04-08 79 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

相关问题