アプリからメールを起動する
アプリからメールを起動する方法。
.hファイル
・メールのフレームワークを読み込む
・メール用のデリゲートをセット
#import <MessageUI/MessageUI.h> @interface MainViewController : UIViewController <MFMailComposeViewControllerDelegate>
.mファイル
//------------------------------------------------------------------------------ // メール機能開始 //------------------------------------------------------------------------------ - (void)mailStart{ // メールを利用できるかチェック if (![MFMailComposeViewController canSendMail]) { return; } //メール管理作成 MFMailComposeViewController *mailCtrl = [[MFMailComposeViewController alloc] init]; //デリゲートの設定 mailCtrl.mailComposeDelegate = self; //タイトル [mailCtrl setSubject:[NSString stringWithFormat:@“メールタイトル”]]; //メールアドレス [mailCtrl setToRecipients:[NSArray arrayWithObjects:@"メールアドレス”, nil]]; //本文 [mailCtrl setMessageBody:@"メール本文” isHTML:NO]; //送信するデータのNSString NSString *str = @"データ用文字列"; //送信するデータをNSData型に変換する NSData *data = [str dataUsingEncoding:NSShiftJISStringEncoding];dataUsingEncoding:NSShiftJISStringEncoding]; //データを添付する [mailCtrl addAttachmentData:data mimeType:@"text/csv" fileName:@“データファイル名.csv”]; //メールビューを表示 [self presentViewController:mailCtrl animated:YES completion:nil]; } //------------------------------------------------------------------------------ // メール機能終了(終了すると呼ばれる) //------------------------------------------------------------------------------ - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { switch (result) { // キャンセル case MFMailComposeResultCancelled: break; // 保存 case MFMailComposeResultSaved: break; // 送信 case MFMailComposeResultSent: { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"送信に成功しました。" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alert show]; break; } // 送信失敗 case MFMailComposeResultFailed: { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"送信に失敗しました。" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alert show]; break; } default: break; } // モーダルビューを閉じる [self dismissViewControllerAnimated:YES completion:nil]; }