使用ios 8 app [swift]打开邮件附件 [英] Open attachment from mail using ios 8 app [swift]
问题描述
在我的应用程序中,我设计了一种新的加密类型数据作为邮件的附件。当我从其他用户收到相同类型的附件(filename.filetype)时,我希望邮件中的附件在我的应用程序中打开。我经历了动作扩展教程。但是,缺少的是,如何使用我的快速应用程序打开该特定类型的附件。我在Obj-C以及以前版本的iOS中得到答案。我在iOS8中寻求答案,Swift处理文件。
这是我的info.plist
<密钥GT; UTExportedTypeDeclarations< /密钥GT;
< array>
< dict>
< key> UTTypeConformsTo< / key>
< array>
< string> public.data< / string>
< / array>
< key> UTTypeDescription< / key>
< string> pvt文件< / 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文件< / string>
< key> CFBundleTypeRole< / key>
< string> Viewer< / string>
< key> LSHandlerRank< / key>
< string>所有者< / 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)
返回true}
首先,您需要声明应用程序将在您的应用程序中处理的文件类型 Info.plist
。 / p>
例如,下面显示的配置声明该应用程序能够打开基本为XML的 .lumenconfig
文件。 有关声明的更多信息,请参阅。 a>
< key> CFBundleDocumentTypes< / key>
< array>
< dict>
< key> CFBundleTypeName< / key>
<! - 此文件类型的名称。 - >
< string>流明配置< / string>
< key> CFBundleTypeIconFiles< / key>
<! - 此文件类型的名称。 - >
< array />
< key> LSItemContentTypes< / key>
<! - 处理
的不同类型标识符作为这种类型的文件。 - >
< array>
<! - 这是我在下面声明的自定义类型 - >
< string> at.zujab.lumen.lumenconfig< / string>
< / array>
< / dict>
< / array>
如果您在上述示例中使用像我这样的自定义类型,您还需要声明该类型。 有关声明您自己的类型的更多信息,请参阅/ a>
< key> UTExportedTypeDeclarations< / key>
< array>
< dict>
< key> UTTypeConformsTo< / key>
<! - 这些文件是如何构造的 - >
< array>
< string> public.xml< / string>
< / array>
< key> UTTypeIdentifier< / key>
<! - 这是自定义类型的标识符 - >
< string> at.zujab.lumen.lumenconfig< / string>
< key> UTTypeDescription< / key>
<! - 你的应用程序如何调用这些文件。 - >
< string>流明配置文件< / string>
< key> UTTypeTagSpecification< / key>
< dict>
< key> public.filename-extension< / key>
<! - 这种类型的文件的扩展名 - >
< string>管理配置< / string>
< / dict>
< / dict>
< / array>
然后在您的应用程序委托中实现一个处理程序来处理文件:
@UIApplicationMain
class AppDelegate:UIResponder,UIApplicationDelegate {
// ....
func应用程序(应用程序:UIApplication,openURL url:NSURL,sourceApplication:String?注释:AnyObject?) - > Bool {
// url包含文件的URL,您的应用程序将打开
//在我的示例中,我想要将该文件作为字典读取
let dictionary = NSDictionary (contentsOfURL:url)
}
}
In my app, I designed a new encrypted type data as attachment to the mail. When I receive the same type of attachment(filename.filetype) from another user, I want the attachment from mail to open in my app. I went through the action extensions tutorials. But, what is missing is, how can I open that particular type of attachment using my swift app. I get the answers in Obj-C as well as the previous versions of iOS. I am seeking an answer in iOS8, Swift to handle the file.
Here is my 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>
Here is my AppDelegate
func application(application: UIApplication, openURL Url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
let encrypteddata = NSData(contentsOfURL: Url)
return true}
First of all you need to declare the file type your app will handle in your apps Info.plist
.
For instance the configuration shown below declares that the app is able to open .lumenconfig
files which are basically 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>
If you use a custom type like I do in the above example you also need to declare that type. 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>
Then in your app delegate implement a handler to handle the file:
@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)
}
}
这篇关于使用ios 8 app [swift]打开邮件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!