Skip to content

Instantly share code, notes, and snippets.

@sahara-ooga
Last active April 18, 2017 02:11
Show Gist options
  • Save sahara-ooga/826654ace2eb8ba38cdebc22c85f836d to your computer and use it in GitHub Desktop.
Save sahara-ooga/826654ace2eb8ba38cdebc22c85f836d to your computer and use it in GitHub Desktop.
メーラを立ち上げてメール送信

ViewControllerに次のように実装しました。

/**
 メール作成ボタンが押されたら呼ばれるメソッド

 @param sender  Storyboardで設定したボタン
 */
- (IBAction)composeButtonTapped:(UIButton *)sender {
    //メールを送信できるかチェック
    if (![MFMailComposeViewController canSendMail]) {
        NSLog(@"Mail services are not available.");
        return;
    }
    
    MFMailComposeViewController* mailViewController = [[MFMailComposeViewController alloc] init];
    
    //delegateプロパティと間違えないように
    mailViewController.mailComposeDelegate = self;
    
    //件名
    [mailViewController setSubject:@"アプリからメール送信"];
    
    //本文
    [mailViewController setMessageBody:@"ここに本文を入力してください。"
                                isHTML:NO];
                                
    [self presentViewController:mailViewController
                       animated:YES
                     completion:nil];
}

#pragma mark MFMailComposeViewControllerDelegate

/**
 デリゲートメソッド。メール作成画面でボタンが押されたら呼ばれる。画面遷移の処理をここに実装する。
 */
- (void) mailComposeController:(MFMailComposeViewController *)controller
           didFinishWithResult:(MFMailComposeResult)result
                         error:(NSError *)error{

    switch (result) {
        case MFMailComposeResultCancelled:{
            NSLog(@"Email Send Cancelled");
            break;
        }
        case MFMailComposeResultSaved:{
            NSLog(@"Email Saved as a Draft");
            break;
        }
        case MFMailComposeResultSent:{
            NSLog(@"Email Sent Successfully");
            break;
        }
        case MFMailComposeResultFailed:{
            NSLog(@"Email send Failed");
            break;
        }
        default:
            break;
    }
    
    [self dismissViewControllerAnimated:YES
                             completion:nil];
}

他のものを設定したい場合(画像など)、 http://qiita.com/kobaboy/items/60a2f44ad53675cfa0ae を参照のこと。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment