我們可以使用電子郵件應(yīng)用程序 iOS 設(shè)備發(fā)送電子郵件(Email)。
涉及的步驟
1. 創(chuàng)建一個(gè)簡(jiǎn)單的 View based application.
2. 選擇項(xiàng)目文件,然后選擇目標(biāo),添加 MessageUI.framework.
3. 在 ViewController.xib 添加一個(gè)按鍵并創(chuàng)建一個(gè)用于發(fā)送電子郵件的動(dòng)作。
4. 更新 ViewController.h 如下
#import <UIKit/UIKit.h> #import <MessageUI/MessageUI.h> @interface ViewController : UIViewController<MFMailComposeViewControllerDelegate> { MFMailComposeViewController *mailComposer; } -(IBAction)sendMail:(id)sender; @end
4. 更新 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
現(xiàn)在,當(dāng)我們運(yùn)行程序時(shí),我們會(huì)得到下面的輸出。
在點(diǎn)擊“發(fā)送電子郵件,我們將得到下面的輸出。