簡體   English   中英

MFMailComposeViewController 崩潰

[英]Crash on MFMailComposeViewController

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

符合:

*** 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 根本不喜歡這樣。

正如提問者在他的問題中報告的那樣, MFMailComposeViewController使用了一些不應分類的原生元素。 因此,您必須確保UITextFieldUITextView等元素沒有任何類別。

我似乎對 iOS MFMailComposeViewController class 的To:字段有疑問。 可能是,您正在發送“To Recipient”字符串數組,它沒有有效的NSString object,並且在內部iOS嘗試使用sizeWithFont for To:字段計算字符串的高度。

只是建議您通過替換以下語句來測試上述假設。

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

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

讓我知道它是否再次崩潰並顯示相同的消息。

編輯:

UITableView 崩潰給出“CALayerInvalidGeometry”,原因:“CALayer position 包含 NaN:[160 nan]”

在帶有 ARC 的 iOS5/6 上呈現視圖 controller 時,我遇到了同樣的問題。 我的解決方案是保留使用 ivar 呈現 MFMailComposeViewController 的 object。

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


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

嘗試使用

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

要么參考這個鏈接:

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

這是我將調試設置為最大值時得到的:

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。 曾經。

所以這對我來說確實有幫助:

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

如果沒有任何幫助,您可以隨時打開郵件至 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];
  1. MFMailComposeViewController 是 UINavigationController 的子類,因此請確保此處的“self”具有 UINavigationController。

  2. 確保您已導入 MessegeUI 框架。

  3. 檢查您是否已確認 class 的.h 文件中的 UINavigationController 協議。

請讓我知道這是否可以解決問題。

謝謝!

伊尚克

如果您將以下類別添加到文本字段,則 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;

我想這會對你有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM