简体   繁体   中英

iOS: How do I detect if an email has been successfully sent?

I am trying to check if an Email message was sent and display an Alert allowing the user know.
I tried the delegate method below , but sadly will display the alert message if user cancels as well. Any help will be appreciated and rewarded.

 - (void)mailComposeController:(MFMailComposeViewController*)controller
      didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{
if (error) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:
                                   [NSString stringWithFormat:@"Error %@", [error description]] delegate:self
                                                   cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [alert show];
}


     NSLog(@"email sent");
    }

}

All that it means when that function is called is that something happened with the email because the MFMailComposeViewController is finished. To know what did actually happen, you have to take a look at the value of result , which can be any of the following:

MFMailComposeResultCancelled
MFMailComposeResultSaved
MFMailComposeResultSent
MFMailComposeResultFailed

As rmaddy says in comments, you can't be 100% sure that the email was actually sent (it could be stuck in the outbox). What MFMailComposeResultSent signifies, then, is that the email has been sent over to the Mail app, which will send it as soon as it can.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM