- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的应用程序遇到了一个奇怪的问题,我需要你的帮助!
我正在使用 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/
我的应用程序处于横向模式。当我通过当前模型 View 调用 MFMailComposeViewController 时,它进入横向模式。我旋转设备并且 MFMailComposeViewControl
我使用 MFMailComposeViewController 在我的应用程序中发送邮件。但是,当当前邮件撰写 View Controller 时,所有导航按钮都被禁用(选择邮件地址屏幕中的后退按钮除
我使用MFMailComposeViewController向其他人发送邮件。单击按钮时,撰写表将打开,我可以输入收件人地址、主题、邮件正文。但点击发送按钮后,邮件页面没有关闭。 代码: if ([M
我正在尝试向我的应用程序添加电子邮件功能。我可以让 MFMailComposeViewController 正确显示并预填充其主题和正文,但由于某种原因,当用户单击导航栏中的“取消”或“发送”按钮时,
我正在使用 MFMailComposeViewController,一切看起来都很好。该类表明邮件已发送,但我从未在模拟器上设置过电子邮件,也不知道如何设置。那么如果发送了,它发送到哪个电子邮件服务器
if ([MFMailComposeViewController canSendMail]) { MFMailComposeViewController *mailViewController
我尝试在我的应用程序中插入邮件工具......我的应用程序基于cocos2d引擎 工具栏(顶部 ->取消、发送...)可见,但我看不到 mfMailComposerViewController Vie
由于某种原因,发送和取消按钮没有出现,我什至在应用程序启动时将以下代码粘贴到 appdelegate 中的应用程序开头,但仍然无法正常工作。有谁可以帮忙吗,谢谢 mailController = [[
MFMailComposeViewController 只有英文版本吗!? 我正在考虑使用 MFMailComposeViewController 来处理从我的应用程序发送电子邮件的想法,但我需要它符
我根据API出示了MFMailComposeViewController,一般情况下,界面是正常的,但是我们应用程序的一些用户提示右上角的发送按钮是灰色的(它变成禁用)的问题,她不能按发送按钮,当然我
如何在不按 MFMailComposeViewController 中的“发送”或“取消”按钮的情况下关闭键盘?! 感谢您的帮助。 最佳答案 你能试试这个吗? UIWindow* keyWindow
我正在开发一个通用应用程序,我正在为 iOS6 编码。 我正在使用 imagePickerController 拍摄照片,然后使用 MFMailComposeViewController 将其作为附件
首先,我使用的是 XCode 4.0.2。 好的,这是我的问题。我可以为 MFMailComposerViewController 构建 Apple 示例程序并在模拟器中运行它(我知道它不会发送电子邮
我已经设置了MFMailComposeViewController,它在iPhone上正常工作,但是在iPad上崩溃了,并说: *** Terminating app due to uncaught
我正在使用 MFMailComposeViewController在具有以下代码的 View Controller 中: if !MFMailComposeViewController.canSend
目前我有一个 NSArray 的电子邮件,我打开一个 View 来结束所有这些电子邮件的电子邮件: MFMailComposeViewController *mailer = [[MFMailComp
我正在使用 MFMailComposeViewController在我的应用程序中撰写反馈电子邮件。 MFMailComposeViewController显示,但无法关闭。 用于打开MFMailCo
我已经按照 Swift Guide 完成了 MFMailComposeViewController() 的常规设置 https://developer.apple.com/library/prerel
我正在尝试在 Swift 中创建以下扩展: extension MFMailComposeViewController { convenience init(document: D
单击警报按钮时尝试使用 MFMailComposeViewController。但是当我单击警报按钮时, Controller 不会弹出。代码如下。我使用了扩展程序,并且在单击警报按钮时尝试调用 se
我是一名优秀的程序员,十分优秀!