step 1 :
- add MessageUI framework
step 2 :
#import "MessageUI/MessageUI.h"
- add MFMailComposeViewControllerDelegate to .h file
step 3 :
- code in .m file on an clicked event of send Button
if ([MFMailComposeViewController canSendMail])
{
// csv file path details for attachments
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filename =@"test.csv";
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:filename];
NSData *myData = [NSData dataWithContentsOfFile:fullPathToFile];
// pdf file path details for attachments
NSString *filename1 =@"NewPDF.pdf";
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:filename1];
NSData *myData1 = [NSData dataWithContentsOfFile:pdfPath];
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
NSMutableArray *arr = [[NSMutableArray alloc]init];
[arr addObject:emailText.text];
[mailer setToRecipients:arr];
[mailer setSubject:@"Vehicle Expenses from myConsultant"];
NSString *emailBody = @"";
[mailer setMessageBody:emailBody isHTML:NO];
[mailer addAttachmentData:myData mimeType:@"text/csv" fileName:filename];
[mailer addAttachmentData:myData1 mimeType:@"text/pdf" fileName:filename1];
[self presentModalViewController:mailer animated:YES];
[mailer release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
message:@"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
step 4 : // add delegate method anywhere in .m class file
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
break;
case MFMailComposeResultSent:
break;
case MFMailComposeResultFailed:
break;
default:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Email" message:@"Sending Failed - Unknown Error :-("
delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
break;
}
[self dismissModalViewControllerAnimated:YES];
}
Only the thing is that you have a file available on a path you given for attachment
so enjoy Love coding, iCoding
No comments:
Post a Comment