This code snippet is a bit longer than usual but it is really useful. It can be added to a view controller to allow sending of mail to a fixed address (although the user can edit it). It could be used for sending feedback from your app.
#import <MessageUI/MessageUI.h>
// Adopt protocol: MFMailComposeViewControllerDelegate
/**
* @brief Sends mail to a hard-coded address.
*/
- (void)sendMail
{
if([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController* mailer = [[MFMailComposeViewController alloc] init];
NSString* to = [NSString stringWithFormat:@"%@@%@", @"snippets", @"iosdeveloperzone.com"];
[mailer setToRecipients:[NSArray arrayWithObject:to]];
[mailer setSubject:@"Snippet Feedback"];
[mailer setMessageBody:@"Nice snippet!" isHTML:NO];
mailer.mailComposeDelegate = self;
[self presentModalViewController:mailer animated:YES];
}
else
{
NSLog(@"Can't send mail");
}
}
// MARK: -
// MARK: MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
UIViewController* parent = controller.parentViewController;
[parent dismissModalViewControllerAnimated:YES];
NSString* message = nil;
switch(result)
{
case MFMailComposeResultCancelled:
message = @"Not sent at user request.";
break;
case MFMailComposeResultSaved:
message = @"Saved";
break;
case MFMailComposeResultSent:
message = @"Sent";
break;
case MFMailComposeResultFailed:
message = @"Error";
}
NSLog(@"%s %@", __PRETTY_FUNCTION__, message);
}
