你好,歡迎來到IOS教程網

 Ios教程網 >> IOS基礎知識 >> IOS入門簡介 >> iOS發送電子郵件

iOS發送電子郵件

編輯:IOS入門簡介

IOS發送電子郵件


簡介

我們可以使用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

輸出

當運行該應用程序,會看如下的輸出結果

sendEmailOutput1

當點擊"send email"發送按鈕後,可以看到如下結果:

sendEmailOutput2

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved