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

时间:2015-08-10 08:57:26

标签: swift email email-attachments ios-app-extension uti

在我的应用中,我设计了一个新的加密类型数据作为邮件的附件。当我从其他用户收到相同类型的附件(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}

1 个答案:

答案 0 :(得分:4)

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

例如,下面显示的配置声明应用程序能够打开基本上是XML的.lumenconfig个文件。 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)

    }

}