Search code examples
objective-ciosmfmailcomposeviewcontroller

Not receiving attachment sent from iOS app / MFMailComposeViewController


I am using MFMailComposeViewController to send attachments (pdfs) from within the app. However I am not receiving the attachments when I test this on a device. Any idea what can be the problem?

- (void) emailDocument 
{
    MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];

    NSData *data = [[NSData alloc] initWithContentsOfURL:pdfURL];


    mailController.mailComposeDelegate = self;

    [mailController setSubject:[self title]];
    [mailController setMessageBody:@"Please find the attached documents." isHTML:YES];
    [mailController addAttachmentData:data mimeType:@"application/pdf" fileName:@"document"];
    [self presentModalViewController:mailController animated:YES];
}

Solution

  • try to understand this code,it helps u.

    - (void)viewDidLoad 
    {
    if ([MFMailComposeViewController canSendMail])
    
        myButton.enabled = YES;
    }
    
    
    - (IBAction)buttonPressed 
    {
    MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
    
        mailController.mailComposeDelegate = self;
    
        [mailController setSubject:@"Hello iPhone"];
    
        [mailController setMessageBody:@"This is the MailSend Application...." isHTML:NO];
    
        [self presentModalViewController:mailController animated:YES];
    
        [mailController release];
    }  
    
    - (void)mailComposeController:(MFMailComposeViewController*)mailController didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
    {
    [self becomeFirstResponder];
    
        [self dismissModalViewControllerAnimated:YES];
    }
    

    Thanks