iphone - 电子邮件 Controller - 触摸取消时崩溃(但不发送)

标签 iphone ios cocoa-touch email

我有一个非常简单的 MFMailComposeViewController 实现。它工作得很好——我从我的应用程序传递一个字符串,该字符串将通过电子邮件发送,然后触摸“发送”,电子邮件就会发送。没问题。

但是,如果我触摸“取消”按钮,应用程序将崩溃 (EXC_BAD_ACCESS)。我是否必须实现特殊方法来取消 MFMailComposeViewController?发送按钮会自动以正确的方式执行,但取消按钮不会。两者之间有什么区别(除了在一种情况下会发送电子邮件而在另一种情况下不会发送电子邮件)?

这是我的代码:

    #pragma mark - EMail

-(IBAction)emailCurrentPage:(id)sender {

    NSString *textToBeSend = @"Test";

    MFMailComposeViewController *mailComposer;

    mailComposer=[[MFMailComposeViewController alloc] init];
    mailComposer.mailComposeDelegate=self;
    [mailComposer setMessageBody:textToBeSend isHTML:NO];
    [self presentModalViewController:mailComposer animated:YES];

    [mailComposer release];

}

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

    if (error) {

        NSString *msgOFF = [[NSString alloc] initWithFormat:@"I could not send the e-mail for the following reason: %@", error];
        UIAlertView *alertOFF = [[UIAlertView alloc]
                                 initWithTitle:@"Error"
                                 message:msgOFF
                                 delegate:self
                                 cancelButtonTitle:@"OK"
                                 otherButtonTitles:nil];
        [alertOFF show];
        [alertOFF release];
        [msgOFF release];

    }

    [self dismissModalViewControllerAnimated:NO];


}

最佳答案

试试这个:

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error  {   
    NSString *message = @"";
    // Notifies users about errors associated with the interface
    switch (result) {
        case MFMailComposeResultCancelled:
            message = @"Mail: canceled";
            break;
        case MFMailComposeResultSaved:
            message = @"Mail: saved";
            break;
        case MFMailComposeResultSent:
        {
            message = @"Mail: sent";

            //Your code


        }
            break;
        case MFMailComposeResultFailed:
            message = @"Mail: failed";
            break;
        default:
            message = @"Mail: not sent";
            break;
    }
    [self dismissModalViewControllerAnimated:YES];
}

关于iphone - 电子邮件 Controller - 触摸取消时崩溃(但不发送),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8008283/

相关文章:

ios - 带有边界限制和动画的平移手势

ios - 使用 AutoLayout 的 iPhone 3.5 和 iPhone 4 英寸中心按钮?

iphone - 在 UITableViewCell 中添加 UITableView。基于内容的TableView高度制作

ios - Swift Metal Shader 命令缓冲区的执行由于在执行期间将数字添加到数组值时出错而中止

ios - 带Open Graph的iMessage中视频的丰富链接预览

iphone - 对于 Cocoa 或 Cocoa-touch 有什么好的矢量库?

iphone - 将 NSMutableArray 传递给函数

javascript - 2019 年及之后如何在移动浏览器 Web 应用程序中访问运动传感器(加速计、陀螺仪)?为什么它不再起作用?

cocoa - NSURL 的参数字符串使用 ';' 与 '&' 发生混淆

iphone - 在导航栏下添加搜索栏?