ios - 使用 MFMailComposeViewControllerDelegate EXE_BAD_ACCESS 出错(代码=1,地址=0x8)

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

    }

最佳答案

您不能让 mailComposeController 自行关闭。您必须拥有呈现它的 UIViewController 才能关闭它。

您需要将委托(delegate)方法移动到您的 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 :-(不要称它为委托(delegate)...它不是委托(delegate)...重构它并删除委托(delegate)一词)

#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 :-(再次强烈建议删除“委托(delegate)”部分)

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

关于ios - 使用 MFMailComposeViewControllerDelegate EXE_BAD_ACCESS 出错(代码=1,地址=0x8),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29009412/

相关文章:

ios - 如何以编程方式将 UISegmentedControl 添加到容器 View

ios - 以编程方式更改 Storyboard的 rootViewController

swift - 如何在 Swift 中为 UIViewController 子类创建自定义初始化程序?

ios - 在 Swift 中将字符串转换为 HTML

iphone - 将生成的文件从ios下载到mac

iphone - uicollection View 可重用单元格图像在滚动时重新加载

ios - 用 UIView 像翻书一样翻页?

ios - 在 iOS 中发出 HTTP 请求的正确位置在哪里?

ios - 引号在数组中丢失

ios - 从 JSON 文件快速加载 NSURL 并将其链接到 imageView