Search code examples
objective-ciosobjective-c-blocksmfmailcomposeviewcontroller

Is completion block syntax for MFMailComposeViewController mailComposeDelegate possible?


I would like to use the MFMailComposeViewController mailComposeDelegateproperty with completion block syntax, but not sure if that is possible. Something similar to how the TWTweetComposeViewController completionHandler property works:

TWTweetComposeViewController __weak *twee = tweeter;
tweeter.completionHandler = ^(TWTweetComposeViewControllerResult result) {
    // code here
};

The code I have creates the MFMailComposeViewController, but then has to save a reference my viewController parameter, since this "share via email" functionality is not in a UIViewcontroller, but a custom MYSharing class.

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
// more code here
_viewController = viewController;
picker.mailComposeDelegate = self;
[viewController presentModalViewController:picker animated:YES];

The mailComposeController:didFinishWithResult:error: from MFMailComposeViewControllerDelegate uses that _viewController to dismiss the modal.

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

Not understanding blocks very well, is there some syntax that look like this?

picker.mailComposeDelegate = ^(???)(??? ??? ???) {
    [viewController dismissModalViewControllerAnimated:YES];
}

I did find MFMailComposeViewController(BlocksKit) (which has source on github), but even with that, the syntax conversion trips me up.


Solution

  • No completion handler for MFMailComposeViewController now.

    if you want to reference the view controller who presented the MFMailComposeViewController, code like below, use presentingViewController property.

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