gpt4 book ai didi

ios - 为什么 MFMailComposeViewController 返回 MFMailComposeResultFailed?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:23:37 26 4
gpt4 key购买 nike

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

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

我的问题是为什么要这样做?我假设内存警告告诉我有些事情没有得到很好的管理,但我无法弄清楚是什么......

这是我发送邮件的代码

-(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 :

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

最佳答案

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

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

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

如果问题是选择器由于内存不足而无法完成附件,而您可以通过提前发布来解决这个问题,那么按以下方式分解代码可能会对您有所帮助。

- (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];
}

关于ios - 为什么 MFMailComposeViewController 返回 MFMailComposeResultFailed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13420514/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com