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

My code crashes on the

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

...

Any idea whats going on?

EDIT: All the answer so far have done nothing, it still crashes :(

EDIT 2: I finally figured out the problem - I had added a category to text field and MFMailComposeViewController did not like that at all.

user754905
  • 1,799
  • 3
  • 21
  • 29

9 Answers9

1

As the asker reported on his question, MFMailComposeViewController uses some native elements that shouldn't be categorized. So, you have to be sure there aren't any categories on elements like UITextField or UITextView.

1

It seems me you have issue with the To: field of iOS MFMailComposeViewController class. May be,you are sending the "To Recipient" string array, which doesn't have the valid NSString object, and internally iOS try to calculate the height of your string using sizeWithFont for To: field .

Just suggest you to test the above assumption by replacing the below statement.

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

With

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

Let me know if it again crash with same message .

EDITED:

UITableView crash gives 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [160 nan]'

Community
  • 1
  • 1
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
  • It did indeed (crashed with the same message). Just to clarify, my EMAIL_SUPPORT is :#define EMAIL_SUPPORT @"stuff@stuff.com" – user754905 Jun 12 '11 at 07:26
  • @user754905 : Does it crash if you comment out all the code related with `MFMailComposeController ` ? – Jhaliya - Praveen Sharma Jun 12 '11 at 07:29
  • Check it http://stackoverflow.com/questions/3025176/what-causes-this-error-calayer-position-contains-nan-240-nan – Jhaliya - Praveen Sharma Jun 12 '11 at 07:33
  • Yes, I commented out everything except the init line and it crashed. The way the code is setup is that I have a UITableViewController subclass, which has a UITableViewCell which is clicked. When it is clicked, it calls that piece of code to present the modal view so that the user can email. – user754905 Jun 12 '11 at 07:34
  • I had already seen that link. I do not have any fancy calculations going on, neither do I have any "bad method names". I will take a look at the NSLog breakpoint stuff tho to see if I can find a better error log. – user754905 Jun 12 '11 at 07:37
  • @ user754905: The issue which you are facing mostly occurred with the UITabelView'd heightForRowAtIndexPath function. plz check my updated answer. – Jhaliya - Praveen Sharma Jun 12 '11 at 07:37
  • Here is my height for row method: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 44.0; } – user754905 Jun 12 '11 at 07:38
  • Tried cleaning. Also tried the NSLog breakpoint stuff. Didn't work. – user754905 Jun 12 '11 at 07:44
0

I've had the same issue when presenting the view controller on iOS5/6 with ARC. My solution was to retain the object that was presenting the MFMailComposeViewController with an ivar.

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


{...
_mailUtils = [[MailUtils alloc] init];
[_mailUtils publish]; //in this method I create the MFMailComposeViewController
}
nalitzis
  • 61
  • 1
  • 2
0

Just try using

[mailViewController setToRecipients:[NSArray arrayWithObjects:EMAIL_SUPPORT,nil]];

either refer to this link:

http://www.edumobile.org/iphone/iphone-programming-tutorials/mailsend-in-the-iphone/

Ajay Sharma
  • 4,509
  • 3
  • 32
  • 59
0

this is what I got with debugging set to max:

bool _WebTryThreadLock(bool), 0x4be8910: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...

slightly different stacktrace though:

#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]
Team Pannous
  • 1,074
  • 1
  • 8
  • 11
0

You must not access UI from any thread besides the main thread. Ever.

So this indeed helped in my case:

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];
});
Team Pannous
  • 1,074
  • 1
  • 8
  • 11
  • Like I said, I would be stupid to access UI from any thread besides main thread lol. But for the sake of "just in case" I tried that code, and yes it did crash still lol. – user754905 Jun 13 '11 at 06:28
0

if nothing helps you can always open a mailto url:

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];
Team Pannous
  • 1,074
  • 1
  • 8
  • 11
  • Yes this is what I have been thinking about doing. I am just kinda curious why it is crashing. If nothing else works, I'll accept this answer (Although it's not a real answer to the problem).. Damn wish I could have ran into this problem 2 days ago; I was at WWDC..blah. – user754905 Jun 13 '11 at 01:48
  • Ended up doing something like this since I couldn't fix. – user754905 Jun 15 '11 at 19:29
0
  1. MFMailComposeViewController is a subclass of a UINavigationController, so make sure that "self" here has a UINavigationController.

  2. Make sure that you have imported the MessegeUI framewok.

  3. Check that you have confirmed to UINavigationController protocol in the .h file of the class.

Please, let me know if that solves the issue.

Thanks!

Ishank

Ishank
  • 2,860
  • 32
  • 43
0

If you added the below category to text field then MFMailComposeViewController did not like that at all. So avoid that. I don't know why. I am writing what I observed.

 - (CGRect)textRectForBounds:(CGRect)bounds {

          return CGRectInset( bounds , 10 , 0 );
    }

But You can add these categories to customize your text field:

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

I think this will help you.

umakanta
  • 1,051
  • 19
  • 25