我們可以使用IOS設備中的電子郵件應用程序發送電子郵件。
1、創建一個簡單的View based application
2、選擇項目文件,然後選擇目標,然後添加MessageUI.framework
3、在ViewController.xib中添加一個按鈕,創建用於發送電子郵件的操作(action)
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
當運行該應用程序,會看如下的輸出結果
當點擊"send email"發送按鈕後,可以看到如下結果: