MFMailComposeViewController 崩溃问题

3
if ([MFMailComposeViewController canSendMail]) {

    MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
    mailViewController.mailComposeDelegate = self;
    [mailViewController setSubject:@"Support Enquiry"];
    [mailViewController setToRecipients:[NSArray arrayWithObject:EMAIL_SUPPORT]];

    [self presentModalViewController:mailViewController animated:YES];
    [mailViewController release];
}

我的代码在该处崩溃:
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init]; 

line with:

*** Terminating app due to uncaught exception 'CALayerInvalidGeometry', 
reason: 'CALayer position contains NaN: [nan 24.5]'
*** Call stack at first throw:
(
0   CoreFoundation                      0x011f75a9 __exceptionPreprocess + 185
1   libobjc.A.dylib                     0x0134b313 objc_exception_throw + 44
2   CoreFoundation                      0x011afef8 +[NSException raise:format:arguments:] + 136
3   CoreFoundation                      0x011afe6a +[NSException raise:format:] + 58
4   QuartzCore                          0x00145ba2 _ZL18CALayerSetPositionP7CALayerRKN2CA4Vec2IdEEb + 177
5   QuartzCore                          0x00145d55 -[CALayer setPosition:] + 42
6   QuartzCore                          0x0013e24d -[CALayer setFrame:] + 763
7   UIKit                               0x0047eda2 -[UIView(Geometry) setFrame:] + 255
8   UIKit                               0x004ea660 -[UITextField setFrame:] + 166
9   MessageUI                           0x001faa48 -[_MFComposeRecipientView reflow] + 3371
10  MessageUI                           0x001f9678 -[_MFComposeRecipientView setLabel:] + 79
11  MessageUI                           0x001ff144 -[MFMailComposeView _setupField:withLabel:navTitle:property:changingView:toSize:fieldFrame:visible:] + 234
12  MessageUI                           0x00203277 -[MFMailComposeView _layoutSubviews:changingView:toSize:searchResultsWereDismissed:] + 1768
13  MessageUI                           0x001fea6f -[MFMailComposeView _layoutSubviews:changingView:toSize:] + 71
14  MessageUI                           0x001fea22 -[MFMailComposeView _layoutSubviews:] + 69
15  MessageUI                           0x00200859 -[MFMailComposeView initWithFrame:navigationItem:options:delegate:] + 2175
16  MessageUI                           0x00215ea6 -[MFMailComposeController initializeUI] + 228
17  MessageUI                           0x0021cfb9 -[MFMailComposeController initForContentSize:navigationItem:options:] + 147
18  MessageUI                           0x00236267 -[MFMailComposeRootViewController initWithCompositionContext:contentSize:mailComposeControllerOptions:] + 377
19  MessageUI                           0x0022fbe7 -[MFMailComposeViewController initWithComposition:contentSize:mailComposeControllerOptions:] + 726
20  MessageUI                           0x0022f634 -[MFMailComposeViewController initWithComposition:] + 68
21  MessageUI                           0x0022f71e -[MFMailComposeViewController initWithNibName:bundle:] + 98

有什么想法是怎么回事吗?

编辑:到目前为止,所有的答案都没有起作用,它仍然崩溃 :(

编辑2:我终于找出了问题所在 - 我已经向文本字段添加了一个类别,而MFMailComposeViewController根本不喜欢。


1
你是否正在使用 sizeWithFont 来计算高度? - Jhaliya - Praveen Sharma
抱歉,代码库中没有sizeWithFont这个函数 : ( - user754905
你在不同的线程上执行这个操作吗? - Deepak Danduprolu
不,它在主线程上。 - user754905
9个回答

1

正如提问者在他的问题中所报告的那样,MFMailComposeViewController使用了一些不应该被分类的本地元素。因此,您必须确保像UITextFieldUITextView这样的元素上没有任何类别。


1

看起来你在 iOS 的 MFMailComposeViewController 类的 To: 字段上遇到了问题。

可能是因为你正在发送一个"To Recipient"字符串数组,其中没有有效的NSString对象,而内部的iOS尝试使用sizeWithFont计算你的字符串的高度,用于To:字段。

建议你通过替换下面的语句来测试上述假设。

[mailViewController setToRecipients:[NSArray arrayWithObject:EMAIL_SUPPORT]];

使用

[mailViewController setToRecipients:[NSArray arrayWithObject:@"myCompanySupport@abc.com"]];

如果它再次崩溃并显示相同的错误信息,请让我知道。

编辑:

UITableView 崩溃,显示“CALayerInvalidGeometry”,原因:“CALayer位置包含NaN:[160 nan]”


确实发生了崩溃并显示相同的消息。只是为了澄清,我的EMAIL_SUPPORT是:#define EMAIL_SUPPORT @"stuff@stuff.com" - user754905
@user754905:如果您注释掉与“MFMailComposeController”相关的所有代码,它是否会崩溃? - Jhaliya - Praveen Sharma
检查一下这个链接 https://dev59.com/wHA75IYBdhLWcg3w790Y - Jhaliya - Praveen Sharma
是的,除了init行之外,我注释掉了所有内容,结果它崩溃了。代码的设置方式是我有一个UITableViewController子类,其中有一个UITableViewCell被点击。当它被点击时,它调用那段代码来呈现模态视图,以便用户可以发送电子邮件。 - user754905
这是我的行高方法:
  • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 44.0; }
- user754905
显示剩余3条评论

0

arrayWithObject 接受1个参数,但我尝试了 arrayWithObjects 却不起作用 - 出现相同的错误。 - user754905

0
  1. MFMailComposeViewController是UINavigationController的子类,所以请确保这里的"self"有一个UINavigationController。

  2. 请确保您已经导入了MessegeUI框架。

  3. 检查您是否在类的.h文件中确认了UINavigationController协议。

请告诉我这是否解决了问题。

谢谢!

Ishank


这段代码片段没问题,控制器肯定有什么问题。 - Ishank

0

这是我在将调试设置为最大时得到的结果:

bool _WebTryThreadLock(bool),0x4be8910:尝试从主线程或Web线程以外的线程获取Web锁。这可能是从辅助线程调用UIKit的结果。现在崩溃了...

尽管堆栈跟踪略有不同:

#2  0x32b69f40 in -[MFComposeBodyField initWithFrame:]
#3  0x32b69afe in -[MFMailComposeView _setupBodyFieldWithHeaderFrame:enclosingFrame:changingView:frameToPin:wasSearching:]
#4  0x32b66c12 in -[MFMailComposeView _layoutSubviews:changingView:toSize:searchResultsWereDismissed:]
#5  0x32b6651e in -[MFMailComposeView _layoutSubviews:changingView:toSize:]
#6  0x001cafe2 in -[MFMailComposeViewAccessibility(SafeCategory) _layoutSubviews:changingView:toSize:]
#7  0x32b664ee in -[MFMailComposeView _layoutSubviews:]
#8  0x32b66202 in -[MFMailComposeView initWithFrame:navigationItem:options:delegate:]
#9  0x32b65876 in -[MFMailComposeController initializeUI]
#10 0x32b656c0 in -[MFMailComposeController initForContentSize:navigationItem:options:]
#11 0x32b6558c in -[MFMailComposeRootViewController initWithCompositionContext:contentSize:mailComposeControllerOptions:]
#12 0x32b64dc2 in -[MFMailComposeViewController initWithComposition:contentSize:mailComposeControllerOptions:]
#13 0x32b64bfe in -[MFMailComposeViewController initWithComposition:]
#14 0x32b8cfb2 in -[MFMailComposeViewController initWithNibName:bundle:]
#15 0x33169a12 in -[UIViewController init]

除了主线程之外,您绝不能从任何其他线程访问UI。永远不要这样做。 - Team Pannous
“debugging set to max” 是什么意思?有什么我可以做的来帮助我隔离这个错误吗? - user754905
FYI:确实在主线程上。我会很傻去在其他线程上访问UI,哈哈。 - user754905

0

除了主线程,您不能从任何其他线程访问UI。永远不要这样做。

所以这确实在我的情况下有所帮助:

dispatch_async(dispatch_get_main_queue(), ^{
    /* Do somthing here with UIKit here */  

MFMailComposeViewController *picker = [[[MFMailComposeViewController alloc] initWithRootViewController:view] autorelease];
picker.mailComposeDelegate = self;  
NSArray *toRecipients = [NSArray arrayWithObject: subject ];    
[picker setToRecipients:to];    
[view presentModalViewController:picker animated:YES];
});

就像我说的那样,如果除了主线程之外的任何线程都访问UI,那我就太蠢了lol。但是为了“以防万一”,我尝试了那段代码,结果还是崩溃了lol。 - user754905

0

如果没有什么帮助,你总是可以打开一个mailto的链接:

body= [(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)body, NULL, CFSTR("?=&+"), kCFStringEncodingUTF8) autorelease];
String uri= [@"mailto:" adds:to];
uri=[[uri adds:@"?subject="] adds:subject];
uri=[[uri adds:@"&body="] adds:body];

是的,这正是我一直在考虑要做的事情。我只是有点好奇为什么它会崩溃。如果其他方法都不起作用,我会接受这个答案(尽管它并不是解决问题的真正答案)...该死,希望我两天前就遇到了这个问题;我当时正在 WWDC 上。 - user754905
由于我无法修复,最终做了这样的事情。 - user754905

0
如果您将以下类别添加到文本字段中,则MFMailComposeViewController会非常不喜欢。所以要避免这种情况。 我不知道为什么,我只是写下了我观察到的事情。
 - (CGRect)textRectForBounds:(CGRect)bounds {

          return CGRectInset( bounds , 10 , 0 );
    }

但是您可以添加这些类别来自定义您的文本字段:

      - (CGRect)borderRectForBounds:(CGRect)bounds;
      - (CGRect)placeholderRectForBounds:(CGRect)bounds;
      - (CGRect)editingRectForBounds:(CGRect)bounds;
      - (CGRect)clearButtonRectForBounds:(CGRect)bounds;
      - (CGRect)leftViewRectForBounds:(CGRect)bounds;
      - (CGRect)rightViewRectForBounds:(CGRect)bounds;

我认为这会对你有所帮助。


0

在iOS5/6中使用ARC时,我遇到了同样的问题,即呈现视图控制器时出现问题。我的解决方案是使用实例变量保留呈现MFMailComposeViewController的对象。

@interface MyClass (){
     MailUtils *_mailUtils;
}
@end


{...
_mailUtils = [[MailUtils alloc] init];
[_mailUtils publish]; //in this method I create the MFMailComposeViewController
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接