MFMailComposeViewController挂起我的应用程序

时间:2010-03-11 19:32:58

标签: iphone mfmailcomposeviewcontroller

我正在尝试向我的应用添加电子邮件功能。我可以让MFMailComposeViewController正确显示并预先填充其主题和正文,但由于某些原因,当用户点击导航栏中的“取消”或“发送”按钮时,应用程序就会挂起。我在mailComposeController"didFinishWithResult:error的第一行插入了一条NSLog()语句,它甚至没有将该行打印到控制台。

有人知道会导致MFMailComposeViewController像这样挂起的原因吗?

以下是标题中的代码:

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

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

- (IBAction)emailObject;
@end

来自实施文件:

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];

这是来自回调的代码:

#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];
}

谢谢!

1 个答案:

答案 0 :(得分:6)

我也有点喜欢,你需要设置mailComposeDelegate,而不是委托。