【问题标题】:Open attachment from mail using ios 8 app [swift] [duplicate]使用 ios 8 应用程序从邮件中打开附件 [swift] [重复]
【发布时间】:2015-11-02 02:56:18
【问题描述】:

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

【问题讨论】:

  • 我无法解决这个 Obj-C。我正在 Swift 中寻求参考或答案。

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


【解决方案1】:

首先,您需要声明您的应用将在您的应用中处理的文件类型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)

    }

}

【讨论】:

  • 这是action Extension的info.plist吗?
  • @MohammedJanish 否。您不需要扩展程序即可在应用程序中打开文件。这是应用程序本身的 Info.plist。
  • public.xmlUTTypeIdentifierat.zujab.lumen.lumenconfigUTTypeDescriptionLumen 配置文件 这些行代表什么?
  • AppDelegate 中要给出的 url 是什么?是邮件App的url吗?
  • @MohammedJanish 我在这里声明了我自己的类型。我正在使用一个名为lumenconfig 的自定义类型,iOS 不知道。当然,URL 指向文件(当您点击电子邮件附件并选择打开方式时)。
猜你喜欢
  • 2018-03-20
  • 2014-11-16
  • 2012-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-30
相关资源
最近更新 更多