views:

318

answers:

1

Hi,

I am trying to add email functionality to my app. I can get the MFMailComposeViewController to display correctly and pre-populate its subject and body, but for some reason when the user clicks on the "Cancel" or "Send" buttons in the nav bar the app just hangs. I inserted a NSLog() statement into the first line of mailComposeController"didFinishWithResult:error and it doesn't even print that line out to the console.

Does anybody have an idea what would cause the MFMailComposeViewController to hang like that?

Here is my code from the header:

#import "ManagedObjectEditor.h"
#import <MessageUI/MessageUI.h>

@interface MyManagedObjectEditor : ManagedObjectEditor 
    <MFMailComposeViewControllerDelegate, UIImagePickerControllerDelegate,
     UINavigationControllerDelegate> {
}

- (IBAction)emailObject;
@end

from the implementation file:

if ([MFMailComposeViewController canSendMail]) {        
    MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
    mailComposer.delegate = self;
    [mailComposer setSubject:NSLocalizedString(@"An email from me",
                                               @"An email from me")];
    [mailComposer setMessageBody:emailString
                          isHTML:YES];
    [self presentModalViewController:mailComposer animated:YES];
    [mailComposer release];
}
[error release];
[emailString release];

and here is the code from the callback:

#pragma mark -
#pragma mark Mail Compose Delegate Methods
- (void)mailComposeController:(MFMailComposeViewController *)controller 
          didFinishWithResult:(MFMailComposeResult)result 
                        error:(NSError *)error {
    NSLog(@"in didFinishWithResult:");
    switch (result) {
        case MFMailComposeResultCancelled:
            NSLog(@"cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"sent");
            break;
        case MFMailComposeResultFailed: {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error sending email!",@"Error sending email!")
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"Bummer",@"Bummer")
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
            break;
        }
        default:
            break;
    }
    [self dismissModalViewControllerAnimated:YES];
}

Thanks!

+1  A: 

I got bit by this too, you need to set the mailComposeDelegate, not the delegate.

Ben Gottlieb
omg, how stupid! Why would Apple use a nonstandard naming convention for a delegate!?!??Fixed the problem though!
Neal L
Since it's subclassing a UINavigationController, delegate was already used.
Ben Gottlieb
+1 as this had caught me out too and isn't the most obvious solution.
davbryn