如何在应用中经过邮件输入和输出数据

这篇文章还能够在这里找到 英语html

Load Attachments In Your App!

Load Attachments In Your App!ios

不少开发者都但愿可以经过电子邮件分享他们的应用数据。这对用户用户之间以及设备之间的数据传输来讲是一个很方便的方法--它甚至能够为你带来一些新的用户。
幸运的是,在iPhone应用开发里这是很容易实现的 -- 你只须要在Info.plist里设置几个key,而且处理几个回传函数使控制系统能够经过URL引入数据来开启应用。
咱们将会在这篇教程里讲解这些是如何实现的。
咱们将会由the Scary Bugs项目开始讲解。咱们在简单应用教程中开始了这个项目,而且在文件分享教程中对它进行了更新。
若是你尚未这个项目,能够在这里下载。
app

设定你的Info.plist

咱们已经为了支持邮件分享应用数据作了许多准备工做--咱们已经写好代码将应用的数据保存了一个独立的副本。
因此下一步须要作的就是设定Info.plist让操做系统知道咱们能够处理”Scary Bug 的文件”。具体的操做就是将你的应用注册为能够处理特定UTI,而且将系统不知道的UTI引出。
总的来讲,UTI就是表明你的文件的惟一标识符,就如同”com.raywenderlich.scarybugs.sbz”同样。一些常见的文件类型,如”public.jpeg”和”public.html”,还有它们本身的内置UTI。
接下来咱们会将应用注册为“能够处理咱们本身生成的UTI的应用”,而后咱们会将UTI的信息告诉操做系统,好比它所使用的文件扩展名以及它在email编码中所使用的MIME类型。
如今让咱们来看看具体怎么操做吧!打开ScaryBugs-Info.plist,而且添加如下条目:
Info.plist Setup for App Documents
你能够从Apple的UTI手册中查到这些值都是什么含义,可是你要注意如下几点:iphone

  • CFBundleDocumentTypes条目的意思是咱们的应用支持那个/些UTI做为全部者/编辑器,在咱们的应用中,即为com.raywenderlich.scarybugs.sbz UTI。编辑器

  • UTExportedTypeDeclaration条目提供了com.raywenderlich.scarybugs.sbz的信息,由于这并非一个公用的UTI。也就是说,若是任何文件的后缀为.sbz或者它的mime类型为application/scarybugs,那么它就属于这个咱们支持的文件类型。函数

信不信由你,设置这些keys就是咱们让操做系统开始传输后缀为.sbz的应用数据所要作的所有准备工做。你能够经过给本身发送一个 sample bug来进行测试。
Emailed Bug Screenshot
若是你想打开Scary Bugs,能够按住附件。若是你这么作了,Scary Bugs就会打开,固然在这里它并不会加载,由于咱们尚未加入任何的代码。这就是咱们接下来要作的事情。测试

引入应用数据

当邮件或者其余应用想要给你的应用发送一个文件,能够经过两个方法:经过 application:didFinishLaunchingWithOptions,用 UIApplicationLaunchOptionsURLKey来传递URL,或者经过application:handleOpenURL。
为了理解这些都是在何时发生的,你能够看看Oliver Drobnick写的一片很是实用的讲解这些方法在何时调用的带图表的文章
如今让咱们开始实现吧--这会很简单,由于咱们已经作了不少的准备工做。ui

将下列改动加入ScaryBugDoc.h:this

// After @interface- (BOOL)importFromURL:(NSURL *)importURL;

将下列代码加入ScaryBugDoc.m:编码

// Add new function- (BOOL)importFromURL:(NSURL *)importURL {
    NSData *zippedData = [NSData dataWithContentsOfURL:importURL];    return [self importData:zippedData];    
}

下列代码加入RootViewController.h:

// After @interface- (void)handleOpenURL:(NSURL *)url;

还有将以下代码加入RootViewController.m:

// New method- (void)handleOpenURL:(NSURL *)url {
    [self.navigationController popToRootViewControllerAnimated:YES];
    ScaryBugDoc *newDoc = [[[ScaryBugDoc alloc] init] autorelease];    if ([newDoc importFromURL:url]) {
        [self addNewDoc:newDoc];       
    }}

最后,将下列代码加入ScaryBugsAppDelegate.m:

// Add at end of application:didFinishLaunchingWithOptionsNSURL *url = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];if (url != nil && [url isFileURL]) {
        [rootController handleOpenURL:url];                
} // Add new method-(BOOL) application:(UIApplication *)application handleOpenURL:(NSURL *)url { 
    RootViewController *rootController = (RootViewController *) [navigationController.viewControllers objectAtIndex:0];    if (url != nil && [url isFileURL]) {
        [rootController handleOpenURL:url];                
    }     
    return YES;
 }

这些都不难理解,可是要注意几点。
首先,咱们在ScaryBugDoc中加入了一个方法来将文件从URL引入。从系统中传给咱们的这个URL实际上咱们应用的目录中的文件的副本。因此咱们用NSData来读取它,并将它传给以前写好的importData方法。
在RootViewController中,咱们弹回到root view controller(除非咱们是在detail view的某处),生成一个新的文件,并从给定的URL引入文件。
在ScaryBugsAppDelegate中,在可以接收到咱们须要的URL的地方,若是这是一个文件URL(而不是队列字符串,咱们也许之后会有专门的教程讲解),咱们会通知root view controller如今能够进行引入了。
如今编译而且运行你的应用吧,若是一切顺利的话,你应该能够打开邮件的附件而且看到引入的应用的bug!
Imported Bug

输出应用数据

引入数据是比较难的部分 -- 输出数据将会简单不少。
在EditBugViewController.h中进行以下改动:

// Add to the top of the file#import <MessageUI/MessageUI.h>// Modify EditBugViewController to have two new protocols: UIActionSheetDelegate and MFMailComposeViewControllerDelegate@interface EditBugViewController : UIViewController <UITextFieldDelegate, RateViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIAlertViewDelegate, UIActionSheetDelegate, MFMailComposeViewControllerDelegate> {

而后在EditBugViewController.m中进行以下改动:

// Replace exportTapped with the following:- (void)exportTapped:(id)sender { 
    UIActionSheet *actionSheet = [[[UIActionSheet alloc]
                                   initWithTitle:@"" 
                                   delegate:self 
                                   cancelButtonTitle:@"Cancel" 
                                   destructiveButtonTitle:nil 
                                   otherButtonTitles:@"Export via File Sharing", @"Export via Email", nil] autorelease];    [actionSheet showInView:self.view];
 }// Add new methods- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {     if (buttonIndex == actionSheet.firstOtherButtonIndex + 0) {         [DSBezelActivityView newActivityViewForView:self.navigationController.navigationBar.superview withLabel:@"Exporting Bug..." width:160];   
        [_queue addOperationWithBlock: ^{
            BOOL exported = [_bugDoc exportToDiskWithForce:FALSE];            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [DSBezelActivityView removeViewAnimated:YES];                if (!exported) {
                    UIAlertView *alertView = [[[UIAlertView alloc] 
                                               initWithTitle:@"File Already Exists!" 
                                               message:@"An exported bug with this name already exists.  Overwrite?" 
                                               delegate:self 
                                               cancelButtonTitle:@"Cancel" 
                                               otherButtonTitles:@"Overwrite", nil] autorelease];                    [alertView show];                }
            }];        }]; 
     } else if (buttonIndex == actionSheet.firstOtherButtonIndex + 1) {         [DSBezelActivityView newActivityViewForView:self.navigationController.navigationBar.superview withLabel:@"Exporting Bug..." width:160];   
        [_queue addOperationWithBlock: ^{
            NSData *bugData = [_bugDoc exportToNSData];            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [DSBezelActivityView removeViewAnimated:YES];                if (bugData != nil) {
                    MFMailComposeViewController *picker = [[[MFMailComposeViewController alloc] init] autorelease];                    [picker setSubject:@"My Scary Bug"];                    [picker addAttachmentData:bugData mimeType:@"application/scarybugs" fileName:[_bugDoc getExportFileName]];                    [picker setToRecipients:[NSArray array]];                    [picker setMessageBody:@"Check out this scary bug!  You'll need a copy of ScaryBugs to view this file, then tap and hold to open." isHTML:NO];                    [picker setMailComposeDelegate:self];                    [self presentModalViewController:picker animated:YES];                    
                }             }];        }]; 
     } }- (void)mailComposeController:(MFMailComposeViewController *)controller
		  didFinishWithResult:(MFMailComposeResult)result
						error:(NSError *)error {
    [self dismissModalViewControllerAnimated:YES];}

在这里咱们改变了输出按键弹出窗口询问用户但愿经过以前同样的文件分享仍是电子邮件来输出。
若是用户选择经过邮箱分享文件,咱们会使用MFMailComposeViewController来生成一个邮件信息,而且加入含有bug数据的附件。是否是很简单?
咱们开始测试前的最后一件事情:右键单击Frameworks,选择”AddExisting Framework…”,并从下拉列表中选择”MessageUI.framework”。
下面就编译运行你的应用吧,你应该已经能够经过邮件自动的从应用输出一个“Scary Bug”了!
Emailing Bug

来点好玩的!

若是你已经作了这么多,何不来点更好玩的?
经过应用来生成一个Scary Bug,而后把它用邮件发送给我,邮箱地址在上图的截图中,我会把它加入这片教程!让咱们看看咱们都制造了什么恐怖的生物!:]
更新: 这是一个由Alex Hedley发来的不怎么恐怖bug!:]

何去何从?

这里 就是咱们这个教程系列到目前为止的代码。
个人计划是Scary Bugs应用就这么多了,可是谁知道呢,也许我会再增长点什么。
同时,若是你有什么问题,评论,或者你想分享你以为什么bug最恐怖,请让我知道!:]

另一篇文章也不错:

http://blog.spritebandits.com/2011/12/14/importing-csv-data-file-into-an-ios-app-via-email-attachment/
http://blog.spritebandits.com/2011/12/21/importing-csv-data-file-into-an-ios-app-via-email-attachment-part-2/
相关文章
相关标签/搜索