如何向现有iOS应用程序添加电子邮件

如何向现有iOS应用程序添加电子邮件,ios,email,tabbar,Ios,Email,Tabbar,我根据苹果的“Tabster”示例代码创建了一个应用程序。该应用运行良好,但我想添加电子邮件到它。我创建了一个电子邮件应用程序。我尝试了我能想到的每一种方法,从教程中学习,或阅读,但它不断崩溃。我在苹果开发者论坛上提出了这个问题,有几个回答是简单的“把文件复制到现有的应用程序上,你应该很好。”很明显,这并不是那么简单。我已经添加了MessageUI框架,并尝试以多种不同的方式复制文件,但我仍然卡住了。今天是星期五,这个问题从星期一起就一直困扰着我。我想第一部分是有两个main.m文件,我试着将它

我根据苹果的“Tabster”示例代码创建了一个应用程序。该应用运行良好,但我想添加电子邮件到它。我创建了一个电子邮件应用程序。我尝试了我能想到的每一种方法,从教程中学习,或阅读,但它不断崩溃。我在苹果开发者论坛上提出了这个问题,有几个回答是简单的“把文件复制到现有的应用程序上,你应该很好。”很明显,这并不是那么简单。我已经添加了MessageUI框架,并尝试以多种不同的方式复制文件,但我仍然卡住了。今天是星期五,这个问题从星期一起就一直困扰着我。我想第一部分是有两个main.m文件,我试着将它们合并,我试着将邮件的main.m文件重命名为emailmain.m。我不知道还能尝试什么

所有关于在iOS应用程序中创建电子邮件的文档和教程都是从创建一个新的应用程序开始的,这让我感到惊讶。我错过了什么?如何将电子邮件添加到功能齐全的应用程序中。我将非常感谢任何关于这个主题的指导、文献链接或教程

在这方面我能得到的任何帮助都将不胜感激。还有一些其他类型的东西,我想添加到它,但我甚至不能得到电子邮件实现到它

谢谢你能提供的帮助。
约翰这是我一直使用的方法。它应该能够简单而干净地添加到任何UIViewController

导入您的框架:

#import <MessageUI/MessageUI.h>

您可以在单击按钮或任何需要的时候调用此方法。它会弹出一个email composer视图,您的用户可以点击发送。

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

导入您的框架:

#import <MessageUI/MessageUI.h>

您可以在单击按钮或任何需要的时候调用此方法。它将弹出一个email composer视图,用户可以在其中点击send。

您需要导入MessageUI框架。无论您想在何处使用它,请将其导入相应的.h文件中,并设置MFMailComposeViewController Delegate

#import <MessageUI/MessageUI.h>
@interface ViewController : UIViewController <MFMailComposeViewControllerDelegate>
添加适当的委派方法,您可以使用该方法在发送电子邮件后解除控制器:

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

您需要导入MessageUI框架。无论您想在何处使用它,请将其导入相应的.h文件中,并设置MFMailComposeViewController Delegate

#import <MessageUI/MessageUI.h>
@interface ViewController : UIViewController <MFMailComposeViewControllerDelegate>
添加适当的委派方法,您可以使用该方法在发送电子邮件后解除控制器:

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

下面是一个创建带有图片作为附件的电子邮件的示例代码。您可以根据需要修改它

-(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];
    }
-(无效)创建电子邮件
{
NSMutableString*emailBody=[[NSMutableString alloc]initWithString:@']retain];
[emailBody appendString:@“一些电子邮件正文文本可以放在此处”

”; UIImage*emailImage=[UIImage ImageName:@“myImageName.png”]; NSData*imageData=[NSData dataWithData:UIImagePNGRepresentation(emailImage)]; NSString*base64String=[imageData base64EncodedString]; [emailBody appendString:[NSString stringWithFormat:@“

”,base64String]]; [emailBody appendString:@”“; NSLog(@“%@”,电子邮件正文); //邮件生成器窗口 MFMailComposeViewController*emailDialog=[[MFMailComposeViewController alloc]init]; emailDialog.mailComposeDelegate=self; [emailDialog setSubject:@“我的内联图像文档”]; [emailDialog setMessageBody:emailBody isHTML:YES]; [self-presentModalViewController:emailDialog动画:是]; [emailDialog发布]; [邮件正文发布]; }
以下是创建以图像作为附件的电子邮件的示例代码。您可以根据需要进行修改

-(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];
    }
-(无效)创建电子邮件
{
NSMutableString*emailBody=[[NSMutableString alloc]initWithString:@']retain];
[emailBody appendString:@“一些电子邮件正文文本可以放在此处”

”; UIImage*emailImage=[UIImage ImageName:@“myImageName.png”]; NSData*imageData=[NSData dataWithData:UIImagePNGRepresentation(emailImage)]; NSString*base64String=[imageData base64EncodedString]; [emailBody appendString:[NSString stringWithFormat:@“

”,base64String]]; [emailBody appendString:@”“; NSLog(@“%@”,电子邮件正文); //邮件生成器窗口 MFMailComposeViewController*emailDialog=[[MFMailComposeViewController alloc]init]; emailDialog.mailComposeDelegate=self; [emailDialog setSubject:@“我的内联图像文档”]; [emailDialog setMessageBody:emailBody isHTML:YES]; [self-presentModalViewController:emailDialog动画:是]; [emailDialog发布]; [邮件正文发布]; }
你不能只复制main.m,然后期待它工作。您需要从中复制代码。下面的答案会有所帮助,但听起来你需要从一个关于使用Objective-C和iOS SDK的非常基本的教程开始。你不能只是复制main.m,然后期望它工作。您需要从中复制代码。下面的答案会有所帮助,但听起来您需要从使用Objective-C和iOS SDK的基础教程开始。