为什么MFMailComposeViewController返回MFMailComposeResultFailed? [英] Why is MFMailComposeViewController returning MFMailComposeResultFailed?

查看:54
本文介绍了为什么MFMailComposeViewController返回MFMailComposeResultFailed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中遇到一个奇怪的问题,需要您的帮助!
我正在使用MFMailComposeViewController发送带有附件数据的电子邮件.附件可以是PDF,CSV或XLS文件.也可以将ZIP文件添加到邮件中.

I'm facing a strange issue in my app and I need your help !
I am using a MFMailComposeViewController to send emails with attachment data. The attachment is either a PDF, a CSV or a XLS file. A ZIP file can also be added to the mail.

在大多数情况下,一切正常,但有时(实际上是经常),当附件为XLS并添加ZIP时,我收到多个内存警告,并且作曲家返回MFMailComposeResultFailed,错误完全没有帮助(只说代码错误1,操作无法完成.(MFMailComposeErrorDomain错误1.)").

Everything works fine in most cases but sometimes (actually quite often), when the attachment is a XLS and a ZIP is added, I receive multiple memory warnings and the composer returns MFMailComposeResultFailed, with an error that doesn't help at all (only saying code error 1, "The operation couldn’t be completed. (MFMailComposeErrorDomain error 1.)").

我的问题是为什么要这样做?我以为内存警告告诉我某些东西管理不善,但我不知道该怎么办...

My question is why does it do that ? I assume the memory warnings are telling me something is not well managed but I can't figure out what...

这是我发送电子邮件的代码

Here is my code for sending the email

-(void) sendMail {

    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
    [self prepareMailPicker:picker];

    NSString *filePath = [self getFilePath:pType];

    NSString *zipFile = [self getZipPath];

    NSString *mimeType;
    int userPhoto = [User getCSVPhoto];

    switch (pType) {
        case EPDF:
            mimeType = @"application/pdf";
            userPhoto = [User getPDFPhoto];
            break;
        case ECSV:
            mimeType = @"text/csv";
            break;
        case EExcel:
            mimeType = @"application/vnd.ms-excel";
            break;

        default:
            break;
    }

    NSData *attachmentData = [NSData dataWithContentsOfFile:filePath];
    [picker addAttachmentData:attachmentData mimeType:mimeType fileName:[filePath lastPathComponent]];

    if (userPhoto == 1 && shouldAddZip) {
        NSData *zipData = [NSData dataWithContentsOfFile:zipFile];
        [picker addAttachmentData:zipData mimeType:@"application/zip" fileName:[zipFile lastPathComponent]];
    }

    shouldAddZip = NO;

    [self presentModalViewController:picker animated:YES];
}

-(void) prepareMailPicker:(MFMailComposeViewController*)picker {

    picker.mailComposeDelegate = (id<MFMailComposeViewControllerDelegate>)self;

    picker.navigationBar.tintColor = grayDark;

    [picker setSubject:[TextManager textForKey:@"EMAIL_SUBJECT"]];

    NSString *email = [[User currentUser] getEmail];

    if (email && ![email isEqualToString:@""])
        [picker setToRecipients:[NSArray arrayWithObject:email]];

    NSString *emailBody = [TextManager textForKey:@"EMAIL_TEXT"];
    [picker setMessageBody:emailBody isHTML:YES];
}

任何帮助将不胜感激!

按照@matt的要求,这里有一条日志证明没有任何内容设置为nil:

as asked by @matt, here is a log to prove that nothing is set to nil :

filePath : /var/mobile/Applications/A57F5CD2-E3FE-4417-8810-D746A22CF434/Documents/iNdF_Export_2012-11-19.xls
zipFile : /var/mobile/Applications/A57F5CD2-E3FE-4417-8810-D746A22CF434/Documents/iNdF_recus_2012-11-19.zip
attachmentData : (NSConcreteData *) <0x1d9c3c20> 53 874 bytes
zipData : (NSConcreteData *) <0x1f989100> 6 838 456 bytes

推荐答案

正如您所说,考虑到您收到的内存警告,问题似乎最可能与内存管理有关.

as you say, the problem seems most probably to do with memory management, given the memory warnings you are receiving.

您的代码将保留第一个文件中对attachmentData的引用计数,即使它获取第二个文件的zipData时也是如此.在内部,选择器可能正在复制该数据……

your code is retaining a reference count to the attachmentData from the first file even as it goes out to get the zipData for the second file. internally, the picker is probably copying that data …

因此,您越能尽早释放对大数据的引用,就越不可能收到内存警告.

so the more you can do to release your references to large data as early as possible, the more likely you are not to get the memory warnings.

,并且如果问题是选择器由于内存不足而无法完成附件,并且您可以通过进行早期发布来解决它,那么按以下方式拆分代码可能会对您有所帮助.

and if the problem is that the picker is unable to finish the attachment due to running out of memory, and you are able to get through it by doing early release, then breaking up the code in the following way may help you.

- (void)sendMailPicker:(MFMailComposeViewController*)picker addAttachmentUsingMimeType:(NSString*)mimeType {
    NSString *filePath = [self getFilePath:pType];
    NSData *attachmentData = [NSData dataWithContentsOfFile:filePath];
    [picker addAttachmentData:attachmentData mimeType:mimeType fileName:[filePath lastPathComponent]];
}

- (void)sendMailAddPhotoUsingPicker:(MFMailComposeViewController*)picker {
    NSString *zipFile = [self getZipPath];
    NSData *zipData = [NSData dataWithContentsOfFile:zipFile];
    [picker addAttachmentData:zipData mimeType:@"application/zip" fileName:[zipFile lastPathComponent]];
}

- (void)sendMail {

    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
    [self prepareMailPicker:picker];

    NSString *mimeType;
    int userPhoto = [User getCSVPhoto];

    switch (pType) {
        case EPDF:
            mimeType = @"application/pdf";
            userPhoto = [User getPDFPhoto];
            break;
        case ECSV:
            mimeType = @"text/csv";
            break;
        case EExcel:
            mimeType = @"application/vnd.ms-excel";
            break;

        default:
            break;
    }

    [self sendMailPicker:picker addAttachmentUsingMimeType:mimeType];
    if (userPhoto == 1 && shouldAddZip) {
        [self sendMailAddPhotoUsingPicker:picker];
    }

    shouldAddZip = NO;

    [self presentModalViewController:picker animated:YES];
}

这篇关于为什么MFMailComposeViewController返回MFMailComposeResultFailed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆