使用 ios 8 app [swift] 打开邮件中的附件

Open attachment from mail using ios 8 app [swift]

在我的应用程序中,我设计了一种新的加密类型数据作为邮件的附件。当我从另一个用户收到相同类型的附件 (filename.filetype) 时,我希望邮件中的附件在我的应用程序中打开。我浏览了动作扩展教程。但是,缺少的是,如何使用我的 swift 应用程序打开特定类型的附件。我在 Obj-C 以及以前版本的 iOS 中得到了答案。我正在 iOS8 中寻求答案,Swift 来处理文件。

这是我的 info.plist

<key>UTExportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.data</string>
        </array>
        <key>UTTypeDescription</key>
        <string>pvt file</string>
        <key>UTTypeIdentifier</key>
        <string>com.pryvateBeta.pvt</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <string>pvt</string>
        </dict>
    </dict>
</array>
<key>CFBundleDocumentsType</key>
<array>
    <dict>
        <key>CFBundleTypeIconFiles</key>
        <array/>
        <key>CFBundleTypeName</key>
        <string>pvt file</string>
        <key>CFBundleTypeRole</key>
        <string>Viewer</string>
        <key>LSHandlerRank</key>
        <string>Owner</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>com.pryvateBeta.pvt</string>
        </array>
    </dict>
</array>

这是我的 AppDelegate

func application(application: UIApplication, openURL Url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
    let encrypteddata = NSData(contentsOfURL: Url)

    return true}

首先,您需要在您的应用程序中声明您的应用程序将处理的文件类型Info.plist

例如,下面显示的配置声明该应用程序能够打开 .lumenconfig 个基本上 XML 的文件。 See this for more info about the declarations.

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeName</key>
        <!-- The name of this file type. -->
        <string>Lumen Configuration</string>
        <key>CFBundleTypeIconFiles</key>
        <!-- The name of this file type. -->
        <array/>
        <key>LSItemContentTypes</key>
        <!-- The different type identifiers that are handled 
             as this type of file. -->
        <array>
            <!-- This is a custom type I declare below -->
            <string>at.zujab.lumen.lumenconfig</string>
        </array>
    </dict>
</array>

如果您像我在上面的示例中那样使用自定义类型,您还需要声明该类型。 See this for more information about declaring your own type

<key>UTExportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeConformsTo</key>
        <!-- How these files are structured -->
        <array>
            <string>public.xml</string>
        </array>
        <key>UTTypeIdentifier</key>
        <!-- This is the identifier for the custom type -->
        <string>at.zujab.lumen.lumenconfig</string> 
        <key>UTTypeDescription</key>
        <!-- How your app calls these files. -->
        <string>Lumen Configuration File</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
             <!-- The extension of the files of this type -->
            <string>lumenconfig</string>
        </dict>
    </dict>
</array>

然后在您的应用委托中实现一个处理程序来处理文件:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    //....

    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
        //url contains a URL to the file your app shall open

        //In my EXAMPLE I would want to read the file as a dictionary
        let dictionary = NSDictionary(contentsOfURL: url)

    }

}