2012-05-11 25 views
0

我创建了一个基于Apple的“Tabster”示例代码的应用程序。该应用程序运行良好,但我想添加电子邮件。我创建了一个电子邮件应用并尝试了所有我能想到的方法,从教程中学习或阅读,但它不断崩溃。我在Apple Dev上提出了这个问题。论坛和几个回应只是“将文件复制到现有的应用程序,你应该是好的。”显然它不是这么简单。我已经添加了MessageUI框架,并试图以许多不同的方式复制这些文件,并且我仍然陷入困境。星期五以来,这个问题已经让我从星期一开始。我想第一部分是有2个main.m文件,我已经尝试过将它们组合起来,我尝试将邮件的main.m文件重命名为emailmain.m。我不知道还有什么可以尝试的。如何添加电子邮件到现有的iOS应用程序

对我来说,令人惊讶的是,所有的文档以及所有关于在iOS应用程序中创建电子邮件的教程都是从创建新应用程序开始的。我错过了什么?如何将电子邮件添加到功能完整的应用程序中。我将不胜感激任何指导,文学链接或有关该主题的教程。

任何帮助我可以得到这将非常感激。还有其他几种类型的东西我想添加到它,但我甚至不能将电子邮件实施到它!

谢谢您的帮助,您可以提供。 约翰

+0

你不能只复制main.m,并期望它工作。您需要从中复制代码。下面的答案会有所帮助,但听起来你需要从使用Objective-C和iOS SDK的非常基础的教程开始。 –

回答

3

这是我一直使用的方法。它应该能够简单而干净地添加到任何UIViewController。

导入你的框架:

#import <MessageUI/MessageUI.h> 

确保您包括您在接口委托:

@interface MyViewController : UIViewController <MFMailComposeViewControllerDelegate> 

然后在您的实现,如果你需要它是一个IBAction为增加这种方法的变化或或类似的东西:

- (void)sendEmail 
{  
    if ([MFMailComposeViewController canSendMail]) { 
     MFMailComposeViewController *email = [[MFMailComposeViewController alloc] init]; 
     email.mailComposeDelegate = self; 

     [email setSubject:@"My Email Subject"]; 

     [email setMessageBody:@"My message body." isHTML:NO]; 

     [self presentModalViewController:email animated:YES]; 

    } else { 
     UIAlertView *emailAlert = [[UIAlertView alloc] initWithTitle:@"Email Failure" message:@"Your device is not configured to send email" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
     [emailAlert show]; 
    } 
} 

你可以在按钮点击或一个你想要的东西。它会拉起一个电子邮件作曲家的视图,你的用户可以发送。

0

您需要导入MessageUI框架。在你想要使用它的地方,将它导入到相应的.h文件中并设置MFMailComposeViewControllerDelegate。

#import <MessageUI/MessageUI.h> 
@interface ViewController : UIViewController <MFMailComposeViewControllerDelegate> 

然后,当你想发送邮件使用下面的代码.m文件:

MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init]; 
mailViewController.mailComposeDelegate = self; 

// Optional Configuration Parameters to make life easier for the user 
[mailViewController setSubject:subjectString]; 
[mailViewController setMessageBody:messageString isHTML:YES]; 
[mailViewController setToRecipients:recipientsArray]; 

// Present the VIew Controller and clean up after ourselves 
[self presentModalViewController:mailViewController animated:YES]; 
[mailViewController release]; 

添加适当的委托方法,你可以用它来关闭该控制器一旦电子邮件发送:

-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { 
    [self dismissModalViewControllerAnimated:YES]; 
} 
0

这里是asample代码创建电子邮件与图像作为附件。您可以根据您的需要修改它。

-(void)createEmail 
    { 
    NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:@"<html><body>"] retain]; 
     [emailBody appendString:@"<p>Some email body text can go here</p>"]; 
     UIImage *emailImage = [UIImage imageNamed:@"myImageName.png"]; 
     NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(emailImage)]; 

     NSString *base64String = [imageData base64EncodedString]; 
     [emailBody appendString:[NSString stringWithFormat:@"<p><b><img src='data:image/png;base64,%@'></b></p>",base64String]]; 
     [emailBody appendString:@"</body></html>"]; 
     NSLog(@"%@",emailBody); 

    //mail composer window 
     MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init]; 
     emailDialog.mailComposeDelegate = self; 
     [emailDialog setSubject:@"My Inline Image Document"]; 
     [emailDialog setMessageBody:emailBody isHTML:YES]; 

     [self presentModalViewController:emailDialog animated:YES]; 
     [emailDialog release]; 
     [emailBody release]; 
    } 
相关问题