Search code examples
iosobjective-cemailmfmailcomposer

MFMailComposerViewController Object is not getting allocated in iPad but works fine in simulator


When ever I alloc/init MFMailComposerViewController it's not allocated, and the object is nil. The app crashed when presenting mailViewController. Here's my code:

MFMailComposeViewController  *picker = [[MFMailComposeViewController alloc] init];

picker.mailComposeDelegate = self;

[picker setSubject:@"Photos"];

NSString *body = [NSString stringWithFormat:@"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"
                  "<html>"
                  "<head>"
                  "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">"
                  "</head>"
                  "<body>"
                  "%@"
                  "</body>"
                  "</html>",[NSString stringWithFormat:@"Picture from %@",APP_NAME]];

[picker setMessageBody:body isHTML:YES];

NSData* data = UIImagePNGRepresentation(self.sharedImage);

[picker addAttachmentData:data mimeType:@"image/png" fileName:APP_NAME];
[self presentViewController:picker animated:YES completion:nil];

Solution

  • Step 1: I think First you need add account from settings of device. If you have not added account from settings then you will get this type of issue.

    And Step 2: Propertise and synthesise your MFMailComposeView ControllerObject