I'm developing an app for Ipad (iOS7.1) in Xcode5 which opens an e-mail modal viewcontroller for sending email by using MFMailComposeViewController
I want to attach a PDF archive to my email this is my code:
Functions.m:
#import <MessageUI/MessageUI.h>
@interface Functions()<MFMailComposeViewControllerDelegate>
@end
@implementation Functions
-(void)sendEmailInViewController:(UIViewController *)viewController {
NSString *emailTitle = @"Hello";
NSArray *toRecipents = [[NSArray alloc]initWithObjects:@"jesus@mymail.com", nil];
NSMutableString *messageBody =[[NSMutableString alloc]init];
[messageBody appendString:@"<p> </p><p> </p><p> </p><p><span style=\"font-size:14px;\"><span style=\"color: rgb(0, 0, 102);\"><span style=\"font-family: arial,helvetica,sans-serif;\"><strong>Jesus</strong><br />Gerente Select / Sucursal Santa Fe<br />Paseo de la Lidia No. 801 Piso 8 Mod. 162<br />Col. Lomas del Pedregal, C.P. 01292, México D.F.<br />Tel: + (55) 8728-0908. Ext. 12832<br />e-mail: jesus@mymail.com</span></span></span></p><p><span style=\"color:#000066;\"><span style=\"font-family: arial,helvetica,sans-serif;\"></span></span></p>"];
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil) {
MFMailComposeViewController * mailView = [[MFMailComposeViewController alloc] init];
mailView.mailComposeDelegate = self;
//Set the subject
[mailView setSubject:emailTitle];
//Set the mail body
[mailView setMessageBody:messageBody isHTML:YES];
[mailView setToRecipients:toRecipents];
NSData *pdfData = [NSData dataWithContentsOfFile:@"google.pdf"];
[mailView addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"google.pdf"];
//Display Email Composer
if([mailClass canSendMail]) {
[viewController presentViewController:mailView animated:YES completion:NULL];
}
}
}
- (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
if (!app) { app = (AppDelegate *)[[UIApplication sharedApplication] delegate]; }
if (!currentSplitViewController) {
currentSplitViewController = (UISplitViewController *) app.window.rootViewController;
}
navController = [currentSplitViewController.viewControllers lastObject];
[[navController topViewController] dismissViewControllerAnimated:YES completion:NULL];
}
@end
on any viewController I call to my method this way:
- (IBAction)showEmail:(id)sender {
[functions sendEmailInViewController:self];
}
this is the screenshot of my mail:
it shows a logo of my pdf file, but when my message is sent, I open my inbox but I just find the signature, but anything attached for viewing... how to attach and send it correctly???
any help I'll appreciate
EDIT: this is the answer: I just had to add this
NSData *pdfData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"google" ofType:@"pdf"]];
[mailView addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"google"];
this is my result: