iPhone アプリケーションからメールを送信するには、以下のタスク リストを実行する必要があります。
ステップ 1:インポート#import <MessageUI/MessageUI.h>
メールを送信するコントローラ クラスにインポートします。
ステップ 2: 以下に示すように、コントローラーにデリゲートを追加します。
@interface <yourControllerName> : UIViewController <MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate>
ステップ 3: メールを送信するための以下のメソッドを追加します。
- (void) sendEmail {
// Check if your app support the email.
if ([MFMailComposeViewController canSendMail]) {
// Create an object of mail composer.
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
// Add delegate to your self.
mailComposer.mailComposeDelegate = self;
// Add recipients to mail if you do not want to add default recipient then remove below line.
[mailComposer setToRecipients:@[<add here your recipient objects>]];
// Write email subject.
[mailComposer setSubject:@“<Your Subject Here>”];
// Set your email body and if body contains HTML then Pass “YES” in isHTML.
[mailComposer setMessageBody:@“<Your Message Body>” isHTML:NO];
// Show your mail composer.
[self presentViewController:mailComposer animated:YES completion:NULL];
}
else {
// Here you can show toast to user about not support to sending email.
}
}
手順 4: MFMailComposeViewController デリゲートを実装する
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(nullable NSError *)error {
[controller dismissViewControllerAnimated:TRUE completion:nil];
switch (result) {
case MFMailComposeResultSaved: {
// Add code on save mail to draft.
break;
}
case MFMailComposeResultSent: {
// Add code on sent a mail.
break;
}
case MFMailComposeResultCancelled: {
// Add code on cancel a mail.
break;
}
case MFMailComposeResultFailed: {
// Add code on failed to send a mail.
break;
}
default:
break;
}
}