Для отправки сообщения электронной почты из приложения ваше устройство должно быть настроено на обслуживание электронной почты.
// importing the MessageUI framework
#import <MessageUI/MessageUI.h>
// adding the delegate functionality to the class (<MFMailComposeViewControllerDelegate>)
@interface ViewController : UIViewController <MFMailComposeViewControllerDelegate> {
}
- (IBAction)sendEMailClick:(id)sender {
//check mail service is configure to your device or not.
if ([MFMailComposeViewController canSendMail]) {
// get a new new MailComposeViewController object
MFMailComposeViewController * composeVC = [MFMailComposeViewController new];
// his class should be the delegate of the composeVC
[composeVC setDelegate:self];
// set a mail subject ... but you do not need to do this :)
[composeVC setSubject:@"This is an optional mail subject!"];
// set some basic plain text as the message body ... but you do not need to do this :)
[composeVC setMessageBody:@"This is an optional message body plain text!" isHTML:NO];
// set some recipients ... but you do not need to do this :)
[composeVC setToRecipients:[NSArray arrayWithObjects:@"first.address@test.com", @"second.address@test.com", nil]];
// Present the view controller modally.
[self presentViewController:composeVC animated:true completion:nil];
} else {
NSLog(@"Mail services are not available or configure to your device");
}
}
после отправки или отмены электронной почты нажмите на MFMailComposeViewController
метод делегата MFMailComposeViewControllerDelegate
- это вызов, поэтому вы можете проверить статус отправки электронной почты.
#pragma mark - MFMailComposeViewControllerDelegate Methode.
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(nullable NSError *)error {
switch (result) {
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail sent failure: %@",error.description);
break;
}
// Dismiss the mail compose view controller.
[controller dismissViewControllerAnimated:true completion:nil];
}