Share attachment from Mail App with Share extension in iOS

十年热恋 提交于 2019-12-04 14:24:57

问题


Now i'm working on the app that should implement share extension to share attachments from 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. 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:

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

Thanks in advance.


回答1:


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. 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):

    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. You can get necessary UTI on https://developer.apple.com

Good Luck with developing of action extensions! Any questions are welcome!



来源:https://stackoverflow.com/questions/36496799/share-attachment-from-mail-app-with-share-extension-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!