📜  iOS-发送电子邮件

📅  最后修改于: 2020-12-08 06:21:56             🧑  作者: Mango


我们可以使用iOS设备的电子邮件应用程序发送电子邮件。

涉及的步骤

步骤1-创建一个简单的基于View的应用程序

步骤2-选择您的项目文件,然后选择目标,然后添加MessageUI.framework

步骤3-ViewController.xib中添加一个按钮,并创建一个用于发送电子邮件的操作。

步骤4-更新ViewController.h如下-

#import 
#import 

@interface ViewController : UIViewController {
   MFMailComposeViewController *mailComposer;
}

-(IBAction)sendMail:(id)sender;

@end

步骤5-如下更新ViewController.m-

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
   [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
   [super didReceiveMemoryWarning];
   // Dispose of any resources that can be recreated.
}

-(void)sendMail:(id)sender {
   mailComposer = [[MFMailComposeViewController alloc]init];
   mailComposer.mailComposeDelegate = self;
   [mailComposer setSubject:@"Test mail"];
   [mailComposer setMessageBody:@"Testing message 
   for the test mail" isHTML:NO];
   [self presentModalViewController:mailComposer animated:YES];
}

#pragma mark - mail compose delegate
-(void)mailComposeController:(MFMailComposeViewController *)controller 
   didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
   
   if (result) {
      NSLog(@"Result : %d",result);
   }
   
   if (error) {
      NSLog(@"Error : %@",error);
   }
   
   [self dismissModalViewControllerAnimated:YES];
}
@end

输出

运行应用程序时,将获得以下输出-

iOS教程

点击发送电子邮件后,我们将获得以下输出-

iOS教程