我有一个支持iOS 5和iOS 6的应用程序。 MFMailComposeViewController在iOS 6中工作正常,但当用户尝试编辑电子邮件正文时,我在iOS 5中崩溃了。可以毫无问题地编辑所有其他字段。 有人见过这个问题吗?
代码如下:
// Displays an email composition interface inside the application. Populates all the Mail fields.
-(void)displayMailComposerSheet {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"xyz"];
NSArray *toRecipients = [NSArray arrayWithObject:@"support@xyz.com"];
[picker setToRecipients:toRecipients];
[picker setMessageBody:@"xyz" isHTML:NO];
// Deprecated in iOS 6.0:
[self presentModalViewController:picker animated:YES];
}
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the
// message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
// Deprecated in iOS 6.0:
[self dismissModalViewControllerAnimated:YES];
}
崩溃日志如下所示:
Crashed Thread: 0
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Application Specific Information:
iPhone Simulator 358.4, iPhone OS 5.1 (iPhone/9B176)
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MFComposeTextContentView setInputAccessoryView:]: unrecognized selector sent to instance 0x8d6b880'
*** First throw call stack:
(0x1da6022 0x1b19cd6 0x1da7cbd 0x1d0ced0 0x1d0ccb2 0x5e4b1 0xb5aa29 0x1d71855 0x1d71778 0xa9f19a 0xaabb03 0x49e084 0x169a798 0x4658fb 0x4675f8 0x45fe29 0x45f133 0x4603bf 0x462a21 0x46297c 0x45b3d7 0x1c01a2 0x1c0532 0x1a6dc4 0x19a634 0x1f39ef5 0x1d7a195 0x1cdeff2 0x1cdd8da 0x1cdcd84 0x1cdcc9b 0x1f387d8 0x1f3888a 0x198626 0x303d 0x2f65)
abort() called
Thread 0 Crashed:
0 libsystem_kernel.dylib 0x99b6e9c6 __pthread_kill + 10
1 libsystem_c.dylib 0x991a5f78 pthread_kill + 106
2 libsystem_c.dylib 0x99196bdd abort + 167
3 libc++abi.dylib 0x027dee78 abort_message + 50
4 libc++abi.dylib 0x027dc89e _ZL17default_terminatev + 34
5 libobjc.A.dylib 0x01b19f17 _objc_terminate + 94
6 libc++abi.dylib 0x027dc8de _ZL19safe_handler_callerPFvvE + 13
7 libc++abi.dylib 0x027dc946 std::terminate() + 23
8 libc++abi.dylib 0x027ddb3e __cxa_rethrow + 83
9 libobjc.A.dylib 0x01b19e15 objc_exception_rethrow + 47
10 CoreFoundation 0x01cdcde0 CFRunLoopRunSpecific + 304
11 CoreFoundation 0x01cdcc9b CFRunLoopRunInMode + 123
12 GraphicsServices 0x01f387d8 GSEventRunModal + 190
13 GraphicsServices 0x01f3888a GSEventRun + 103
14 UIKit 0x00198626 UIApplicationMain + 1163
15 SomeXYZApp 0x0000303d main + 141 (main.m:16)
16 SomeXYZApp 0x00002f65 start + 53
答案 0 :(得分:1)
1如果您尝试在设备上执行此操作,请创建邮件帐户
2 MFMailComposeViewController包含methon“canSendMail,所以你可以尝试:
if ([MFMailComposeViewController canSendMail]) {
// create mail
} else {
// error message
}
3尝试在主线程中调用控制器:
dispatch_async(dispatch_get_main_queue(), ^{
//call MFMailComposeViewController
});
答案 1 :(得分:1)
有同样的问题。仅在IOS6中,而不在IOS5.1中。 测试了解到表单未正确发布。因此我添加了代码以强制释放表单。这适用于我的情况。希望它也能帮到你。
-(void) sendMail:(NSString *) mailTo
{
// Check if you can send an e-mail
if ([MFMailComposeViewController canSendMail])
{
@try
{
// show the form
MFMailComposeViewController *mailForm = nil;
mailForm = [[MFMailComposeViewController alloc] init];
mailForm.mailComposeDelegate = self;
[mailForm setToRecipients:@[ mailTo ]];
mailForm.modalPresentationStyle = UIModalPresentationFormSheet;
mailForm.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:mailForm animated:YES];
}
@catch (NSException *exception)
{
// Show what went wrong
NSString *fout = [[NSString alloc] initWithFormat:@"%@: %@",[exception name], [exception reason]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Mailer" message:fout delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
else
{
// Inform the user you cannot send mail, no mailbox set
[self noEmailAccountMeansNoEmail];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)mailForm didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Get the mail form off the screen
if ((mailForm != nil))
[mailForm dismissModalViewControllerAnimated:YES];
// Force release/dealloc of the screen
mailForm = nil;
return;
}
答案 2 :(得分:0)
再次尝试这可能是你忘了小事......我不确定那.....你的代码看起来很好......它应该有效..
首先添加并导入MessageUI Framework
#import <MessageUI/MessageUI.h>
并声明MFMaileComposeViewControllerDelegate
@interface MailViewController : UIViewController <MFMailComposeViewControllerDelegate>
编写此代码以发送邮件
- (IBAction)openMail:(id)sender
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:@"xyz"];
NSArray *toRecipients = [NSArray arrayWithObjects:@"fisrtMail@example.com", @"secondMail@example.com", nil];
[mailer setToRecipients:toRecipients];
NSString *emailBody = @"xyz";
[mailer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailer animated:YES];
[mailer release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
message:@"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
}
并编写MFMailComposeViewControllerDelegate的委托方法
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved: you saved the email message in the drafts folder.");
break;
case MFMailComposeResultSent:
NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");
break;
default:
NSLog(@"Mail not sent.");
break;
}
// Remove the mail view
[self dismissModalViewControllerAnimated:YES];
}
答案 3 :(得分:0)
我发现了这个问题。我正在使用DAKeyboardControl,它允许滚动解除键盘(请参阅GitHub for DA's KeyboardControl)。
当通过MFMailComposeViewController调用DA代码中的responderDidBecomeActive时,textField会收到一个不响应inputAccessoryView的MFComposeTextContentView对象。这似乎是一种不受支持的互动。但请注意,在iOS 6中,由于MFMailComposeViewController未调用DA的重载类,因此不会出现此问题。
如果删除了DAKeyboardControl支持,一切正常。