Saturday 2 November 2013

Presenting a MFMailComposeViewController in a Preference Bundle

So, you want to throw up a modal mail form in your Preference Bundle for your tweak, perhaps in a support section? Sweet! Thankfully it's exceedingly simple; there's only one change to consider when comparing to Apple's example code for presenting a MFMailComposeViewController.


-(void)composeSupportEmail:(id)sender {
    NSString *emailTitle = @"Title";
    NSString *messageBody = @"Message";
    NSArray *toRecipents = [NSArray arrayWithObject:@"foo@bar.com"];
    
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:emailTitle];
    [mc setMessageBody:messageBody isHTML:NO];
    [mc setToRecipients:toRecipents];
    
    // Present mail view controller on screen
    [self.parentController presentModalViewController:mc animated:YES];
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    [self.parentController dismissModalViewControllerAnimated:YES];
}

In the code above, all that has been changed is to use the parentController property inherited from PSViewController as the view to call presentModalViewController:animated: from. And that's all there is to it!

No comments:

Post a Comment