▼iPhoneプログラミングメモ▼
メールの送信

メールを送信するプログラムを作成する。


フレームワークの準備
MessageUI.frameworkを追加。

ソースコードの記述
MailEx.h
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

//MailExの宣言
@interface MailEx : UIViewController <
    UITextFieldDelegate,
    MFMailComposeViewControllerDelegate> {
    UITextField* _textField;
}
@end

MailEx.m
#import "MailEx.h"

#define BTN_SEND 0

//MailExの実装
@implementation MailEx

//アラートの表示
- (void)showAlert:(NSString*)title text:(NSString*)text {
    UIAlertView* alert=[[[UIAlertView alloc] 
        initWithTitle:title message:text delegate:nil 
        cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [alert show];
}

//テキストフィールドの生成
- (UITextField*)makeTextField:(CGRect)rect text:(NSString*)text {
    UITextField* textField=[[[UITextField alloc] init] autorelease];
    [textField setText:text];
    [textField setFrame:rect];
    [textField setReturnKeyType:UIReturnKeyDone];
    [textField setBackgroundColor:[UIColor whiteColor]];
    [textField setBorderStyle:UITextBorderStyleRoundedRect];
    [textField setDelegate:self];
    return textField;
}

//テキストボタンの生成
- (UIButton*)makeButton:(CGRect)rect text:(NSString*)text tag:(int)tag {
    UIButton* button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:rect];
    [button setTitle:text forState:UIControlStateNormal];
    [button setTag:tag];
    [button addTarget:self action:@selector(clickButton:) 
        forControlEvents:UIControlEventTouchUpInside];
    return button;
}

//メール送信
- (void)sendMail:(NSString*)text image:(UIImage*)image {
    //メール送信可能かどうかのチェック
    if (![MFMailComposeViewController canSendMail]) {
        [self showAlert:@"" text:@"メール送信できません"];
        return;
    }

    //メールコントローラの生成
    MFMailComposeViewController* pickerCtl=
        [[MFMailComposeViewController alloc] init];
    pickerCtl.mailComposeDelegate=self;
    
    //メールのテキストの指定
    [pickerCtl setMessageBody:text isHTML:NO];
    
    //メールの添付ファイルの追加
    if (image!=nil) {
        NSData* data=UIImagePNGRepresentation(image);
        [pickerCtl addAttachmentData:data mimeType:@"image/png" 
            fileName:@"sample.png"];
    }

    //メールコントローラのビューを開く
    [self presentModalViewController:pickerCtl animated:YES]; 
}

//メール送信完了時に呼ばれる
- (void)mailComposeController:(MFMailComposeViewController*)controller 
    didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    if (error!=nil) [self showAlert:@"" text:@"メール送信失敗しました"];

    //オープン中のビューコントローラを閉じる
    [self dismissModalViewControllerAnimated:YES];
}

//初期化
- (void)viewDidLoad {
    [super viewDidLoad];

    //テキストフィールドの生成
    _textField=[[self makeTextField:CGRectMake(0,0,300,32) 
        text:@""] retain];
    [self.view addSubview:_textField];
        
    //メール送信ボタンの生成
    UIButton* btnSend=[self makeButton:CGRectMake(0,50,90,40) 
        text:@"メール送信" tag:BTN_SEND];
    [self.view addSubview:btnSend];
}

//メモリ解放
- (void)dealloc {
    [_textField release];
    [super dealloc];
}

//テキストフィールドのリターン時に呼ばれる
- (BOOL)textFieldShouldReturn:(UITextField*)sender {
    [sender resignFirstResponder];
    return YES;
}

//ボタンクリック時に呼ばれる
- (IBAction)clickButton:(UIButton*)sender {
    if (sender.tag==BTN_SEND) {
        UIImage* image=[UIImage imageNamed:@"sample.png"];
        [self sendMail:_textField.text image:image];    
    }
}

//画面を端末の向きにあわせて回転
- (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)orientation {
    return YES;
}
@end



−戻る−