Created
May 4, 2015 17:32
-
-
Save AlexHedley/6e306d13653113d8fcc6 to your computer and use it in GitHub Desktop.
Send an Email
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <MessageUI/MessageUI.h> | |
| @interface ViewController : UIViewController <MFMailComposeViewControllerDelegate> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - (void)sendEmail { | |
| // Email Subject | |
| NSString *emailTitle = @"Subject"; | |
| // Email Content | |
| NSString *messageBody = @"\n\nSent from the iOS App"; | |
| // To address | |
| //NSArray *toRecipents = [NSArray arrayWithObject:kEmail]; | |
| MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; | |
| mc.mailComposeDelegate = self; | |
| [mc setSubject:emailTitle]; | |
| [mc setMessageBody:messageBody isHTML:NO]; | |
| //[mc setToRecipients:toRecipents]; | |
| //NSData *pdfData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"X" ofType:@"pdf"]]; | |
| NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
| NSString *documentsDirectory = [paths objectAtIndex:0]; | |
| NSString *path = [documentsDirectory stringByAppendingPathComponent:@"X.pdf"]; | |
| NSData *pdfData = [NSData dataWithContentsOfFile:path]; | |
| [mc addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"X"]; | |
| // Present mail view controller on screen | |
| [self presentViewController:mc animated:YES completion:^{ | |
| [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; | |
| }]; | |
| } | |
| - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(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 localizedDescription]); | |
| break; | |
| default: | |
| break; | |
| } | |
| // Close the Mail Interface | |
| [self dismissViewControllerAnimated:YES completion:NULL]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment