简体   繁体   English

在iOS中使用Mail扩展名共享Mail App中的附件

[英]Share attachment from Mail App with Share extension in iOS

Now i'm working on the app that should implement share extension to share attachments from Mail App. 现在,我正在开发应实现共享扩展以从Mail App共享附件的应用程序。 It should support different file extensions (almost all types of documents). 它应该支持不同的文件扩展名(几乎所有类型的文档)。 From Apple docs I understood that i have to use Predicate in my Info.plist, but in answers on SO i found that I have to use it in code. 从Apple文档中,我了解到我必须在Info.plist中使用Predicate,但在SO的答案中,我发现我必须在代码中使用它。 Now I'm stuck on that and can't proceed further. 现在,我一直坚持下去,无法继续进行下去。 Here is the Predicate that I want to use from this post . 这是我要在这篇文章中使用的谓词。

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"
            )
).@count == $extensionItem.attachments.@count
).@count == 1

Can anyone advise how to use this predicate in my swift code: 谁能在我的快速代码中建议如何使用此谓词:

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

Here is my NSExtensionActivationRule: 这是我的NSExtensionActivationRule:

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

Thanks in advance. 提前致谢。

So finally I found an answer on my question! 所以最后我找到了问题的答案! Just in case if somebody will meet the same problem.. First of all I have to use PREDICATE statement (Subquery) in Info.plist instead of key NSExtensionActivationSupportsAttachmentsWithMaxCount. 以防万一有人遇到相同的问题。首先,我必须在Info.plist中使用PREDICATE语句(子查询),而不要使用键NSExtensionActivationSupportsAttachmentsWithMaxCount。 Like: 喜欢:

        <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"
            )
            ).@count == 1   // Important! to activate extension only on 1 chosen image
            ).@count == 1
        </string>

Second: Properly get all attachments using necessary TypeIdentifier (UTI): 第二:使用必要的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" - is universal UTI to support all kind of file extensions listed in your NSExtensionActivationRule string. “ public.item” -是通用的UTI,用于支持NSExtensionActivationRule字符串中列出的所有文件扩展名。 You can get necessary UTI on https://developer.apple.com 您可以在https://developer.apple.com上获得必要的UTI。

Good Luck with developing of action extensions! 祝您开发动作扩展好运! Any questions are welcome! 任何问题都欢迎!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM