0

我有一个同时支持 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
4

4 回答 4

1

1 如果您尝试在设备上执行此操作,请创建邮件帐户

2 MFMailComposeViewController 包含methon“canSendMail,所以你可以尝试:

if ([MFMailComposeViewController canSendMail]) {
 // create mail
} else {
// error message
}

3 尝试在主线程中调用控制器:

dispatch_async(dispatch_get_main_queue(), ^{
    //call MFMailComposeViewController
});
于 2012-10-21T07:42:26.523 回答
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;
}
于 2013-01-15T13:19:11.370 回答
0

再试一次可能是你忘记了小事......我不确定......你的代码看起来不错......它应该可以工作......

首先添加并导入 MessageUI 框架

#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];
}
于 2012-10-21T15:27:33.603 回答
0

我发现了问题。我正在使用 DAKeyboardControl 允许滚动关闭键盘(请参阅GitHub 获取 DA 的 KeyboardControl)。

当通过 MFMailComposeViewController 调用 DA 代码中的 responderDidBecomeActive 时,textField 接收到不响应 inputAccessoryView 的 MFComposeTextContentView 对象。这似乎是不支持的交互。但是请注意,在 iOS 6 中,这个问题不会发生,因为 DA 的重载类不被 MFMailComposeViewController 调用。

如果删除了 DAKeyboardControl 支持,则一切正常。

于 2012-10-27T22:06:37.507 回答