Search code examples
xcodeipadios6

attachment not sent with email from ipad


I have an iPad that has a routine to create a pdf and send as an attachment to an email. It all seems to work with the email composer opening showing the pdf document attached. However when tested on an iPad, when the email is received there is no attachment. Any ideas?

    [mailComposer addAttachmentData:data mimeType:@"application/pdf" fileName:@"pdffile.pdf"];
    [self presentViewController:mailComposer animated:YES completion:nil];

Many thanks

Detail:

The pdf file is created and called pdffile.pdf. The following is the full email routine:

    MFMailComposeViewController *mailComposer;
    mailComposer  = [[MFMailComposeViewController alloc] init];
    mailComposer.mailComposeDelegate = self;
    [mailComposer setModalPresentationStyle:UIModalPresentationFormSheet];
    [mailComposer setSubject:[NSString stringWithFormat: @"i-observe Lesson Observation for: %s", "date"]];
    [mailComposer setMessageBody:[NSString stringWithFormat: @"i-observe Lesson Observation for: %s", "name"] isHTML:NO];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *file = [documentsDirectory stringByAppendingFormat:@"pdffile.pdf"];
    NSMutableData *data=[NSMutableData dataWithContentsOfFile:file];
    [mailComposer addAttachmentData:data mimeType:@"application/pdf" fileName:@"pdffile.pdf"];
    [self presentViewController:mailComposer animated:YES completion:nil];

Solution

  • Try This Method:

     if([MFMailComposeViewController canSendMail]){      
    
            MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
            mail.mailComposeDelegate=self;
            [mail setSubject:@"Email with attached pdf"];   
            NSString *newFilePath = @"get path where the pdf reside";
    
            NSData * pdfData = [NSData dataWithContentsOfFile:newFilePath];
        [mail addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"yourpdfname.pdf"];
            NSString * body = @"";
            [mail setMessageBody:body isHTML:NO];
            [self presentModalViewController:mail animated:YES];
            [mail release];         
        }
        else
        {
            NSLog(@"Message cannot be sent");
        }