When we want to use the iOS' email composer from our app, first thing to do is to link MessageUI framework with our binaries, and obviously import it in our header file.
#import <MessageUI/MessageUI.h>
For using it, you have to implement in your class the MFMailComposeViewControllerDelegate
@interface MyViewController : UIViewController <MFMailComposeViewControllerDelegate>
After that, to invoke the MFMailComposer is so easy, just instance it and preload all the email fields that you want: To, cc, bcc, subject... and present it as a modal view controller. Don't forget setting the mailComposeDelegate property to the class which implements MFMailComposeViewControllerDelegate, usually the same class that invokes MFMailComposeViewController.:
MFMailComposeViewController *mailView = [[MFMailComposeViewController alloc] init];
mailView.mailComposeDelegate = self;
mailView.navigationBar.tintColor = [UIColor colorWithRed:0.36078431 green:.015686275 blue:.0078431373 alpha:1];
[mailView setToRecipients:[NSArray arrayWithObject:@"fiveflamesmobile.blogspot.com"]];
[self presentModalViewController:mailView animated:YES];
[mailView release];
After those steps we have to implement de MFMailComposrDelegateProtocol as this:
#pragma mark -
#pragma mark MFMailComposeViewControllerDelegate
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissModalViewControllerAnimated:YES];
UIAlertView *alert = nil;
if (result == MFMailComposeResultSent)
{
alert = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"EmailEnviado", "") message:NSLocalizedString(@"EmailExito", "") delegate:self cancelButtonTitle:NSLocalizedString(@"Aceptar", "") otherButtonTitles:nil] autorelease];
} else if (result == MFMailComposeResultSaved) {
alert = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"EmailGuardado", "") message:NSLocalizedString(@"EmailBorradores", "") delegate:self cancelButtonTitle:NSLocalizedString(@"Aceptar", "") otherButtonTitles:nil] autorelease];
} else if (result == MFMailComposeResultFailed) {
alert = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"EmailNoEnviado", "") message:NSLocalizedString(@"EmailFallo", "") delegate:self cancelButtonTitle:NSLocalizedString(@"Aceptar", "") otherButtonTitles:nil] autorelease];
}
[alert show];
}
Be careful, I've user localizated string to show the message in the alert view, but maybe I'll write about that in next code snippets. And with this few lines of code you will be able to send emails with the iOS mail client.
So that's all for today. More and better (I'll try it) the next week.
No comments:
Post a Comment