iOS - 发送电子邮件



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

涉及的步骤

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

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

步骤 3 - 在ViewController.xib 中添加一个按钮,并为发送电子邮件创建一个动作。

步骤 4 - 按如下所示更新ViewController.h -

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

@interface ViewController : UIViewController<MFMailComposeViewControllerDelegate> {
   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 Tutorial

点击“发送电子邮件”时,将获得以下输出 -

iOS Tutorial
广告