gpt4 book ai didi

iphone - 如何防止 MFMailComposeViewController 取消时崩溃?

转载 作者:行者123 更新时间:2023-12-03 18:54:33 25 4
gpt4 key购买 nike

某处:

if([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *email_vc = [[MFMailComposeViewController alloc] init];
email_vc.mailComposeDelegate = self;

[email_vc setSubject:subject];
[email_vc setMessageBody:message isHTML:FALSE];
[email_vc setToRecipients:recipients];

[self presentModalViewController:email_vc animated:FALSE];
[[UIApplication sharedApplication] setStatusBarHidden:TRUE];
[email_vc release];
}
else
...

其他地方:

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Cancelled");
break;

case MFMailComposeResultSaved:
NSLog(@"Saved");
break;

case MFMailComposeResultSent:
NSLog(@"Sent");
break;

case MFMailComposeResultFailed:
NSLog(@"Compose result failed");
break;

default:
NSLog(@"Default: Cancelled");
break;
}

// This ugly thing is required because dismissModalViewControllerAnimated causes a crash
// if called right away when "Cancel" is touched.

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC), dispatch_get_current_queue(), ^
{
[self dismissModalViewControllerAnimated:FALSE];
});

}

那个丑陋的“dispatch_after” block 是我让它在不崩溃的情况下工作的唯一方法。

上下文是,触摸电子邮件撰写 View Controller 上除“发送”之外的任何内容都会导致崩溃。有没有办法解决这个问题,而不必求助于这个丑陋的创可贴?我关于创可贴的理论是,当您触摸“取消”以确认用户确实想要取消时,会出现一个中间 View 。我想知道 [self DismissModalViewControllerAnimated:FALSE]; 是否试图取消不按顺序的 View 或类似的效果。通过插入一个小的延迟,我推测邮件撰写 View 在被要求消失之前有时间进行清理。

我在另一个问题中看到了延迟。但作者没有详细说明:

Crash On MFMailComposeViewController For iPad

编辑1:添加崩溃日志

Incident Identifier: ****************
CrashReporter Key: *****************
Hardware Model: iPhone4,1
Process: ************* [9038]
Path: /var/mobile/Applications/*********************
Identifier: ***********************
Version: ??? (???)
Code Type: ARM (Native)
Parent Process: launchd [1]

Date/Time: 2012-07-20 11:25:53.704 -0700
OS Version: iPhone OS 5.0.1 (9A405)
Report Version: 104

Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0xa003853a
Crashed Thread: 0

Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libobjc.A.dylib 0x316b9fbc 0x316b6000 + 16316
1 UIKit 0x350caa9e 0x34f8e000 + 1297054
2 UIKit 0x34fa6814 0x34f8e000 + 100372
3 UIKit 0x34fabfb2 0x34f8e000 + 122802
4 QuartzCore 0x33354ba0 0x33329000 + 179104
5 libdispatch.dylib 0x37896f74 0x37894000 + 12148
6 CoreFoundation 0x37bac2d6 0x37b20000 + 574166
7 CoreFoundation 0x37b2f4d6 0x37b20000 + 62678
8 CoreFoundation 0x37b2f39e 0x37b20000 + 62366
9 GraphicsServices 0x376adfc6 0x376aa000 + 16326
10 UIKit 0x34fbf73c 0x34f8e000 + 202556
11 ***************** 0x00002346 main (main.m:14)
12 ***************** 0x00002304 start + 32

编辑 2:经过一番绞尽脑汁,这似乎是一个真正的 Apple bug。

我下载并运行了 MailComposer 示例项目:

http://developer.apple.com/library/ios/#samplecode/MailComposer/Introduction/Intro.html

效果很好。

然后我编辑了代码以在呈现和关闭邮件撰写 Controller 时删除动画。

[self presentModalViewController:picker animated:FALSE];

[self dismissModalViewControllerAnimated:FALSE];

果然,当使用“取消”关闭电子邮件撰写 UI 时,它崩溃了。

运行僵尸带来了这个:

-[MFMailComposeController actionSheet:didDismissWithButtonIndex:]: message sent to deallocated instance 0x7479ef0

我猜操作表会收到解雇消息,而不是邮件撰写 View Controller 。

如果有人可以确认行为,我会报告该错误。

编辑 3:报告错误。

我接受的答案很好地解释了导致此问题的潜在机制。此外,在回答评论的来回过程中,还确定了两个额外的解决方法。所有创可贴,但现在有几个选择。

我还没有检查过,但我怀疑 ShareKit 也受到这个错误的影响(如果邮件撰写 View Controller 的呈现不是动画的)。

最佳答案

I guess the action sheet gets the dismiss message instead of the mail compose view controller.

不完全是。

事件的发生顺序可能是这样的:

  • 操作表在其委托(delegate)(MFMCVC)上调用 -actionSheet:clickedButtonAtIndex:
    • MFMailComposeViewController 在其委托(delegate)(您的 VC)上调用 -mailComposeController:didFinishWithResult:error:
      • 您的 VC 调用[selfmissModalViewControllerAnimated:NO]
        • 这会导致 MFMCVC 被释放。由于解雇不是动画的,因此不再有任何涉及 MFMCVC 的内容。它被释放了!
  • 操作表在其委托(delegate)上调用 -actionSheet:didDismissWithButtonIndex:
    • 但是它的委托(delegate)已被释放!
      • 所以它崩溃了!

Apple 的修复方法是在 -dealloc 中执行 actionSheet.delegate = nil

潜在的解决方法

[[self.modalViewController retain] autorelease]
[self dismissModalViewControllerAnimated:NO]

如果您使用 ARC,这会有点棘手。

关于iphone - 如何防止 MFMailComposeViewController 取消时崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11584607/

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