Search code examples
iosmfmailcomposeviewcontroller

MFMailComposeViewController - Unable to presentViewController


Here is my code

-(IBAction)emailButtonPressed :(UIButton *)sender {

    if (![MFMailComposeViewController canSendMail]) {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"Mail has not been set up on this device" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alertView show];

        return;
}

    NSString *targetFile = [self saveCompleteImage];

    MFMailComposeViewController *mailpicker = [[MFMailComposeViewController alloc] init];
    [mailpicker setMailComposeDelegate:self];

    NSString *mimeType = [StringHelper getMimeType:targetFile];
    [mailpicker addAttachmentData:[NSData dataWithContentsOfFile:targetFile] mimeType:mimeType fileName:[targetFile lastPathComponent]];

    [mailpicker setSubject:[self.currentDocument getNameForUntitled]];
    mailpicker.modalPresentationStyle = UIModalPresentationFormSheet;

    [self.presentedViewController presentViewController:mailpicker animated:YES completion:nil];
}

It's not presenting mailpicker. Kindly tell me where am i wrong.


Solution

  • Issue is with this code:

    [self.presentedViewController presentViewController:mailpicker animated:YES completion:nil];
    

    You need to use:

    [self presentViewController:mailpicker animated:YES completion:nil];
    

    or

    [self.presentingViewController presentViewController:mailpicker animated:YES completion:nil];
    

    Check ModalViewControllers Reference to understand the difference between presentedViewController and presentingViewController