通过iOS中的共享扩展程序从Mail App共享附件 [英] Share attachment from Mail App with Share extension in iOS

查看:464
本文介绍了通过iOS中的共享扩展程序从Mail App共享附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在开发应该实现共享扩展以从Mail App共享附件的应用程序。它应该支持不同的文件扩展名(几乎所有类型的文档)。从Apple docs我明白我必须在我的Info.plist中使用Predicate,但在SO的答案中我发现我必须在代码中使用它。现在我坚持不懈,无法继续前进。以下是我要使用的谓词发布

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

任何人都可以建议如何在我的swift代码中使用这个谓词:

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

这是我的NSExtensionActivationRule:

Here is my NSExtensionActivationRule:

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

提前致谢。

推荐答案

所以我终于在我的问题上找到了答案!以防万一有人会遇到同样的问题..
首先,我必须在Info.plist中使用PREDICATE语句(子查询)而不是键NSExtensionActivationSupportsAttachmentsWithMaxCount。喜欢:

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>

第二:使用必要的TypeIdentifier(UTI)正确获取所有附件:

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 - 是通用UTI,支持NSExtensionActivationRule字符串中列出的所有类型的文件扩展名。
你可以在 https://developer.apple.com

"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!

这篇关于通过iOS中的共享扩展程序从Mail App共享附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆