单击取消按钮时,邮件撰写不会被忽略

时间:2014-07-27 14:36:04

标签: ios objective-c mfmailcomposeviewcontroller

我有这个代码向用户发送电子邮件,我正在使用MFMailCompose:

.h文件

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

@interface TestViewController : UIViewController <MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>

@property(nonatomic,assign) id<MFMailComposeViewControllerDelegate> mailComposeDelegate;

.m文件

@synthesize mailComposeDelegate

-(void)sendEmail:(NSString*)valor{

//Valor receive the email

    NSString *corpoMensagem = @"No have body yet...";

    // From within your active view controller
    if([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
        mailCont.mailComposeDelegate = self;// Required to invoke mailComposeController when send

        [mailCont setSubject:@"FazerBem - Recibo de Compra"];
        [mailCont setToRecipients:[NSArray arrayWithObject:valor]];
        [mailCont setMessageBody:corpoMensagem isHTML:YES];

        [self presentViewController:mailCont animated:YES completion:nil];
    }

}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {

    if (error){
        NSString *errorTitle = @"Erro to send";
        NSString *errorDescription = [error localizedDescription];
        UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:errorTitle message:errorDescription delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [errorView show];
    }

    [self becomeFirstResponder];
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{


}

当我点击发送按钮时,他可以将电子邮件发送给收件人并关闭屏幕,现在当我点击取消按钮时,当我点击一个时,它会再打开2个选项'删除草稿'和'保存草稿'应用程序崩溃并返回以下错误:

[TestViewController respondsToSelector:]: message sent to deallocated instance 0x16b3b470

我如何解决这个问题?

3 个答案:

答案 0 :(得分:3)

使用Switch case执行操作:

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    UIAlertView *alert;
switch (result)
{
    case MFMailComposeResultCancelled:
        break;
    case MFMailComposeResultSaved:
        alert = [[UIAlertView alloc] initWithTitle:@"Draft Saved" message:@"Composed Mail is saved in draft." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        break;
    case MFMailComposeResultSent:
        alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"You have successfully referred your friends." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        break;
    case MFMailComposeResultFailed:
        alert = [[UIAlertView alloc] initWithTitle:@"Failed" message:@"Sorry! Failed to send." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        break;
    default:
        break;
}

// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:nil];
}

答案 1 :(得分:1)

删除此行: [self becomeFirstResponder];

那应该解决你的问题。

答案 2 :(得分:0)

如果以上解决方案不适合您,请在此处使用自我而不是使用 MFMailComposeViewController实例

而不是

[self dismissViewControllerAnimated:YES completion:nil];

使用

[mailComposer dismissViewControllerAnimated:YES completion:nil];