How to send emails continuously in Xcode

Medium 464484822

photo credit: Warm 'n Fuzzy via photopin cc

Good evening, this is Bono.
I was making an application and wanted to send emails continuously, so I made it.

Simply put, immediately after pressing the Send Mail button, the next mail screen appears again.
And when you send that email, you get another email screen, and so on. And so on.

You can ask Dr. google as many ways as you want to call up the mail screen, but there was no continuous mail sending, so I'll leave it there.

How to send e-mails consecutively

The method is quite simple: use the viewDidAppear method.
This method is called each time a view is displayed.
Take advantage of it.

code

Prepare sendCount as a global variable in the header.

MainViewController.h

#import <UIKit/UIKit.h>
#import "SKPSMTPMessage.h"
#import "NSData+Base64Additions.h"

@interface MainViewController : UIViewController <SKPSMTPMessageDelegate> {
    NSInteger sendCount;
}

After that, add 1 each time it is called to that sendCount.

MainViewController.m

- (void)viewDidAppear:(BOOL)animated
{
    if (sendCount == 0){
        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc]init];
        picker.mailComposeDelegate = self;
        [picker setSubject:@"タイトル1です"];
        NSArray *toRecipients = [NSArray arrayWithObject:@"mail@address"];
        [picker setToRecipients:toRecipients];
        NSString *body = @"body1 here";
        [picker setMessageBody:body isHTML:NO];
        [self presentViewController:picker animated:YES completion:nil];
        sendCount++;
    }else if (sendCount == 1){
        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc]init];
        picker.mailComposeDelegate = self;
        [picker setSubject:@"タイトル2です"];
        NSArray *toRecipients = [NSArray arrayWithObject:@"mail@address"];
        [picker setToRecipients:toRecipients];
        NSString *body = @"body2 here";
        [picker setMessageBody:body isHTML:NO];
        [self presentViewController:picker animated:YES completion:nil];
        sendCount++;
    }else if (sendCount == 2){
        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc]init];
        picker.mailComposeDelegate = self;
        [picker setSubject:@"タイトル3です"];
        NSArray *toRecipients = [NSArray arrayWithObject:@"mail@address"];
        [picker setToRecipients:toRecipients];
        NSString *body = @"body3 here";
        [picker setMessageBody:body isHTML:NO];
        [self presentViewController:picker animated:YES completion:nil];
        sendCount = 0; //終了
    }
}

attention (heed)

At first, when I tried to do it in a different way, I got the following warning, but I couldn't figure out why and got a little stuck.
I changed my method a little and took this time and it stopped appearing.

It seems to be some kind of warning that appears when calling other viewControllers during dismiss.

Warning: Attempt to present モーダル on 親のview while a presentation is in progress!

Reference Site
Warning: Attempt to present 〜 on 〜 while a presentation is in progress! | X Ranker

Try it.

When we actually tried sending e-mail, we discovered that it was surprisingly comfortable.
It could be used when you want to send a large number of e-mails by creating the address and body of the e-mail in advance.
Furthermore, it is easy to use and can be slightly edited before sending.