// // MailViewController.m // Mail // // Created by david on 13-8-2. // Copyright (c) 2013年 WalkerFree. All rights reserved. //
#import "MailViewController.h"
@interfaceMailViewController ()
@end
@implementationMailViewController
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. }
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
- (IBAction)showMailPicker:(id)sender { if([MFMailComposeViewController canSendMail]){ [self displayMailComposerSheet]; }else{ NSLog(@"Device not configured to send SMS."); } }
- (void)displayMailComposerSheet { MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; [picker setSubject:@"Hello from California!"]; // Set up recipients NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"]; NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]; NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"]; [picker setToRecipients:toRecipients]; [picker setCcRecipients:ccRecipients]; [picker setBccRecipients:bccRecipients]; // Attach an image to the email NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"]; NSData *myData = [NSData dataWithContentsOfFile:path]; [picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy"]; // Fill out the email body text NSString *emailBody = @"It is raining in sunny California!"; [picker setMessageBody:emailBody isHTML:NO]; [self presentViewController:picker animated:YES completion:NULL]; }
#pragma mark - delegate Methods
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{ // Notifies users about errors associated with the interface switch (result) { case MFMailComposeResultCancelled: NSLog(@"Result: Mail sending canceled") ; break; case MFMailComposeResultSaved: NSLog(@"Result: Mail saved") ; break; case MFMailComposeResultSent: NSLog(@"Result: Mail sent") ; break; case MFMailComposeResultFailed: NSLog(@"Result: Mail sending failed") ; break; default: NSLog(@"Result: Mail not sent") ; break; } [self dismissViewControllerAnimated:YES completion:NULL]; }