使用MFMailComposeViewControllerDelegate EXE_BAD_ACCESS获取错误(代码= 1,地址= 0x8)

时间:2015-03-12 12:09:59

标签: ios iphone uiviewcontroller exc-bad-access mfmailcomposeviewcontroller

我正在尝试将邮件编辑器作为帮助程序类。它从viewcontrollerviewDidLoad方法调用时显示窗口。但是当我按取消然后按删除草稿。该应用程序正在崩溃。崩溃后转到主文件并显示以下错误消息。

EXE_BAD_ACCESS (code=1, address=0x8).

我已经尝试了所有可能的事情,但仍然没有得到任何解决方案。

任何建议将不胜感激!提前谢谢。

以下是代码。

ViewController.m: -

- (void)viewDidLoad {
    [super viewDidLoad];

    //Allocating mailing object.
    EmailManagerDelegate *MailOperation=[[EmailManagerDelegate alloc] init]; 

    //Email Subject
    MailOperation.emailTitle=@"just trying mail composer";
    //Email content.
    MailOperation.messageBody=@"hallo world";
    //To address
    MailOperation.toRecipent=[NSArray arrayWithObject:@"abc@gmail.com"];

    //Setting the name of the File with Format in nstring.
    MailOperation.FileNameWithFormat=@"Demo.txt";

    //Passing the UIViewCOntroller for opeing the mailclient and closing it.
    MailOperation.ViewVontroller=self;

    [MailOperation emailMultiAttachAndSendLog:nil fileName: nil];

}

EmailManagerDelegate.h: -

#import <Foundation/Foundation.h>
#import <MessageUI/MessageUI.h>

@interface OSMEmailManagerDelegate : NSObject <MFMailComposeViewControllerDelegate>

@property (readwrite) NSString* FileNameWithFormat;
@property (nonatomic, strong) UIViewController* ViewVontroller;
//@property (nonatomic) UIViewController* ViewVontroller;
@property (nonatomic,strong) MFMailComposeViewController *mc;
@property (readwrite)NSString* emailTitle;
@property (readwrite)NSString* messageBody;
@property (readwrite)NSArray* toRecipent;

-(void)emailMultiAttachAndSendLog:(NSString*)documentsDirectory fileName:(NSString*)fileWithFormat;

EmailManagerDelegate.m: -

 #import "OSMEmailManagerDelegate.h"

    @implementation OSMEmailManagerDelegate

    @synthesize mc,ViewVontroller;

    -(void)emailMultiAttachAndSendLog:(NSString*)documentsDirectory  fileName:(NSString*)fileWithFormat{


        if ([MFMailComposeViewController canSendMail])
            // The device can send email.
        {

         //Creating a  mail composer
         mc=[[MFMailComposeViewController alloc] init];
        [mc setMailComposeDelegate:self];
        [mc setSubject:_emailTitle];
        [mc setMessageBody:_messageBody isHTML:NO];
        [mc setToRecipients:_toRecipent];
        [mc setModalPresentationStyle: UIModalPresentationFormSheet];

        NSData *fileData;
        NSString *mimeType;
        NSString *filesName;
        if (documentsDirectory && fileWithFormat) {
            //Files section
            NSArray *filePart=[fileWithFormat componentsSeparatedByString:@"."];
            filesName=[filePart objectAtIndex:0];
            NSString *fileExtention=[filePart objectAtIndex:1];

            //Getting and creating path of the zip file.
            NSString *fileArchivePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileWithFormat];

            //Geting the resource file and path
            fileData=[NSData dataWithContentsOfFile:fileArchivePath];

            //Determining the MIME type.
            mimeType=[self getMIMEType:fileExtention];

            //Add attachment
            [mc addAttachmentData:fileData mimeType:mimeType fileName:filesName];
         }

            //Present mail view controller on screen.
            [ViewVontroller presentViewController:mc animated:YES completion:nil];

        }
        else
            // The device can not send email.
        {
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"Device not configured to send mail." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
        }

    }

    //Get the type of MIME with file extention.
    -(NSString *)getMIMEType:fileExtention{
        NSString *mimeType=@"";
        if ([fileExtention isEqualToString:@"csv"]) {
            mimeType=@"text/csv";
        }
        else if ([fileExtention isEqualToString:@"jpg"]) {
            mimeType = @"image/jpeg";
        } else if ([fileExtention isEqualToString:@"png"]) {
            mimeType = @"image/png";
        } else if ([fileExtention isEqualToString:@"doc"]) {
            mimeType = @"application/msword";
        } else if ([fileExtention isEqualToString:@"ppt"]) {
            mimeType = @"application/vnd.ms-powerpoint";
        } else if ([fileExtention isEqualToString:@"html"]) {
            mimeType = @"text/html";
        } else if ([fileExtention isEqualToString:@"pdf"]) {
            mimeType = @"application/pdf";
        } else if ([fileExtention isEqualToString:@"json"]) {
            mimeType = @"text/json";
        } else if ([fileExtention isEqualToString:@"zip"]) {
            mimeType = @"application/zip";
        }
        return mimeType;
    }


    -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
        NSString *status;
        switch(result){
            case MFMailComposeResultCancelled:
                status=@"Mail cancelled";
                break;
            case MFMailComposeResultSaved:
                status=@"Mail saved";
                break;
            case MFMailComposeResultSent:
                status=@"Mail sent";
                break;
            case MFMailComposeResultFailed:
                status=@"Mail sent failure : %@",[error localizedDescription];
                break;
            default:
                break;

        }
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:status delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];

        //Close mail interface.
        [ViewVontroller dismissViewControllerAnimated:YES completion:NULL];

    }

2 个答案:

答案 0 :(得分:1)

你不能让mailComposeController解雇自己。你必须有UIViewController来呈现它以消除它。

您需要将委托方法移动到您的UIViewController ...

这是我重新编写代码的夏天答案:

@interface UIViewController : UIViewController <MFMailComposeViewControllerDelegate>

- (void)viewDidLoad {
[super viewDidLoad];

//Allocating mailing object.
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init]; 

    if ([MFMailComposeViewController canSendMail])
        // The device can send email.

    mc=[[MFMailComposeViewController alloc] init];
    [mc setMailComposeDelegate:self];
    [mc setSubject:@"hello world"];
    [mc setMessageBody:@"hello world" isHTML:NO];
    [mc setToRecipients:[NSArray arrayWithObject:@"abc@gmail.com"]];
    [mc setModalPresentationStyle: UIModalPresentationFormSheet];

    // This is the data you could handle in your custom class... I am   leaving this code as is, fix it in the way your logic works. 
    [MailOperation emailMultiAttachAndSendLog:nil fileName: nil];

            //Present mail view controller on screen.
        [self presentViewController:mc animated:YES completion:nil];

     } else {
           // The device can not send email.
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"Device not configured to send mail." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
     }
}

    -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    NSString *status;
    switch(result){
        case MFMailComposeResultCancelled:
            status=@"Mail cancelled";
            break;
        case MFMailComposeResultSaved:
            status=@"Mail saved";
            break;
        case MFMailComposeResultSent:
            status=@"Mail sent";
            break;
        case MFMailComposeResultFailed:
            status=@"Mail sent failure : %@",[error localizedDescription];
            break;
        default:
            break;

    }
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:status delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];

    //Close mail interface.
    [self dismissViewControllerAnimated:YES completion:NULL];

}

EmailManagerDelegate.h: - (不要称它为委托......它不是代表它的......重构它并删除委托一词)

#import <Foundation/Foundation.h>
#import <MessageUI/MessageUI.h>

@interface OSMEmailManagerDelegate : NSObject 

@property (readwrite) NSString* FileNameWithFormat;
// @property (nonatomic, strong) UIViewController* ViewVontroller;
//@property (nonatomic) UIViewController* ViewVontroller;
//@property (nonatomic,strong) MFMailComposeViewController *mc;
//@property (readwrite)NSString* emailTitle;
//@property (readwrite)NSString* messageBody;
//@property (readwrite)NSArray* toRecipent;

//-(void)emailMultiAttachAndSendLog:(NSString*)documentsDirectory fileName:(NSString*)fileWithFormat;

EmailManagerDelegate.m: - (再次强烈建议删除&#39;代表&#39;部分)

 #import "OSMEmailManagerDelegate.h"

@implementation OSMEmailManagerDelegate

//Get the type of MIME with file extention.
-(NSString *)getMIMEType:fileExtention{
    NSString *mimeType=@"";
    if ([fileExtention isEqualToString:@"csv"]) {
        mimeType=@"text/csv";
    }
    else if ([fileExtention isEqualToString:@"jpg"]) {
        mimeType = @"image/jpeg";
    } else if ([fileExtention isEqualToString:@"png"]) {
        mimeType = @"image/png";
    } else if ([fileExtention isEqualToString:@"doc"]) {
        mimeType = @"application/msword";
    } else if ([fileExtention isEqualToString:@"ppt"]) {
        mimeType = @"application/vnd.ms-powerpoint";
    } else if ([fileExtention isEqualToString:@"html"]) {
        mimeType = @"text/html";
    } else if ([fileExtention isEqualToString:@"pdf"]) {
        mimeType = @"application/pdf";
    } else if ([fileExtention isEqualToString:@"json"]) {
        mimeType = @"text/json";
    } else if ([fileExtention isEqualToString:@"zip"]) {
        mimeType = @"application/zip";
    }
    return mimeType;
}

答案 1 :(得分:0)

以下行导致异常。

EmailManagerDelegate *MailOperation=[[EmailManagerDelegate alloc] init];

请注意,MailOperation是一个局部变量,只存在于viewDidLoad,因此当您点击发送按钮时,它就不再存在。

将其更改为ViewController内的全局变量应该有效。