Ravee10
Ravee10

Reputation: 355

How to use MFMailComposerViewController on iOS 7?

I am developing an app for iOS 7 and used MFMailComposerViewController.

I have tried everything but dismissViewController:withAnimated is not working.

sometimes class automatically call delegate by itself when it first displays viewController using method presentViewCOntroller:withAnimated:completion.

My app is navigation based that's why I think issue is just related with UINavigationController as well.

       -(void)sendMail{

if ([MFMailComposeViewController canSendMail]) {

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

    [picker setSubject:@"Hello from California!"];

    // Set up recipients
    NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"];
    NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];
    NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"];

    [picker setToRecipients:toRecipients];
    [picker setCcRecipients:ccRecipients];
    [picker setBccRecipients:bccRecipients];



    // Fill out the email body text
    NSMutableString *emailBody =[NSMutableString stringWithString: @"<table border=1 align=\"center\"><tr><th>EventDate</th><th>EventDay</th><th>EventTime</th><th>Speaker</th><th>topic</th></tr>"];

    for (int i=0; i<5; i++) {
        NSString *eventDate=[NSString stringWithFormat:@"<tr><td>%@</td>",@"12/11"];
        NSString *eventDay=[NSString stringWithFormat:@"<td>%@</td>",@"Sunday"];
        NSString *eventTime=[NSString stringWithFormat:@"<td>%@</td>",@"12:10 pm"];
        NSString *eventSpeaker=[NSString stringWithFormat:@"<td>%@</td>",@"RajVeer"];
        NSString *eventTopic=[NSString stringWithFormat:@"<td>%@</td>",@"nano-technology"];
        NSString *dataString=[NSString stringWithFormat:@"%@%@%@%@%@</tr>",eventDate,eventDay,eventTime,eventSpeaker,eventTopic];
        [emailBody appendString:dataString];
    }
    NSString *lastTable=@"</table>";
    [emailBody appendString:lastTable];
    NSLog(@"%@",emailBody);
    [picker setMessageBody:emailBody isHTML:YES];

    [self presentViewController:picker animated:YES completion:NULL];


}

Upvotes: 1

Views: 2242

Answers (2)

Robocop Klaus
Robocop Klaus

Reputation: 11

This should do the trick:

#pragma mark MFMailComposeViewControllerDelegate

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    [controller dismissViewControllerAnimated:YES completion:nil];
}

Upvotes: 1

Hemant Singh Rathore
Hemant Singh Rathore

Reputation: 2139

Use this code to present MFMailComposeViewController

[self presentViewController:mailComposerObject animated:YES completion:NULL];

For dismiss MFMailComposeViewController

#pragma mark - MFMailComposeViewControllerDelegate
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult: (MFMailComposeResult)result error:(NSError*)error {
[self dismissViewControllerAnimated:YES completion:NULL];
}

from iOS 6.0 [self presentModalViewController:<#(UIViewController *)#> animated:<#(BOOL)#>] is deprecated.

Upvotes: 0

Related Questions