Rakesh Thummar
Rakesh Thummar

Reputation: 197

How to attach a PDF file to mail and send mail in iOS

Currently I am developing an expense management app and I want to attach a PDF file to the mail and send it to the specified email Address. So guide me from where should I start this and how to develop it.

Thanks.

MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];
vc.mailComposeDelegate = self;

[vc setSubject:@"Monthly Expense Report"];
[vc addAttachmentData:pdfData mimeType:@"application/pdf" fileName:[NSString stringWithFormat:@"%@.pdf",[arrOfExpenseDate objectAtIndex:1]]];
if ([MFMailComposeViewController canSendMail])
{
    [self presentViewController:vc animated:YES completion:nil];
}

Upvotes: 1

Views: 1621

Answers (2)

Naga Mallesh Maddali
Naga Mallesh Maddali

Reputation: 1005

I have created a example for you on how to send an pdf attachment using MFMailComposeViewController. Following is the code :

@interface ViewController () <MFMailComposeViewControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (IBAction)compseEmail:(id)sender
{
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];
        vc.mailComposeDelegate = self;

        [vc setSubject:@"Monthly Expense Report"];


        NSString *pdfPath = [[NSBundle mainBundle] pathForResource:@"ApacheInstallationSteps" ofType:@"pdf"];
        NSData *pdfData = [NSData dataWithContentsOfFile:pdfPath];
        [vc addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"ApacheInstallationSteps.pdf"];

        [self presentViewController:vc animated:YES completion:nil];
    }
    else
    {
        UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Alert!!!" message:@"Your device can't send emails." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

        [errorAlert show];
    }
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    // Notifies users about errors associated with the interface
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Result: canceled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Result: saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Result: sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Result: failed");
            break;
        default:
            NSLog(@"Result: not sent");
            break;
    }

    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

Make sure that you add MessageUI.framework. Following is the output.

enter image description here

Upvotes: 4

z22
z22

Reputation: 10083

To send a pdf or any other attahcment in mail using MFMailComposeViewController, you have to use:

[mc addAttachmentData:fileData mimeType:mimeType fileName:filename];

For more details, follow this tutorial.

You can also download the sample code given in this tutorial for better understanding.

Hope that helps.

Upvotes: 0

Related Questions