4

我在我的 conde 中使用 MFMailComposeViewController 来提供邮件功能,但是在发送邮件之后或者当我想取消邮件时它会崩溃。

下面是我的代码:

(IBAction)FnForPlutoSupportEmailButtonPressed:(id)sender {
{
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];

        mailer.mailComposeDelegate = self;

        [mailer setSubject:@"Need help from Pluto support team"];

        NSArray *toRecipients = [NSArray arrayWithObjects:@"support@myplu.to",nil];
        [mailer setToRecipients:toRecipients];


        NSString *emailBody = @"";

        [mailer setMessageBody:emailBody isHTML:NO];

        //mailer.modalPresentationStyle = UIModalPresentationPageSheet;

        [self presentModalViewController:mailer animated:YES];

    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
                                                        message:@"Your device doesn't support the composer sheet"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles: nil];
        [alert show];
    }
} }

    (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface    
switch (result)
    {       case MFMailComposeResultCancelled:
                        break;
                    case MFMailComposeResultSaved:
                        break;
                    case MFMailComposeResultSent:
                        break;
                    case MFMailComposeResultFailed:
                        break;
                    default:
                        break;
    } 
    [self dismissModalViewControllerAnimated:YES];
     }

我已经阅读了所有博客文章,但没有找到解决方案,这篇博客文章对此有很好的解释,但据此我没有在 viewdidload 或 viewdidappear 中展示我的视图控制器。

我正在获取 EXE_BAD_ACCESS,以下是崩溃日志:

**

> #0  0x00000000 in ?? ()
> #1  0x01dc5aa4 in -[UIViewController _setViewAppearState:isAnimating:] ()
> #2  0x01dc5f47 in -[UIViewController __viewDidDisappear:] ()
> #3  0x01dc6039 in -[UIViewController _endAppearanceTransition:] ()
> #4  0x01dd2e7e in -[UIViewController(UIContainerViewControllerProtectedMethods) endAppearanceTransition] ()
> #5  0x01fc8de1 in -[UIWindowController transitionViewDidComplete:fromView:toView:] ()
> #6  0x01da334b in -[UITransitionView notifyDidCompleteTransition:] ()
> #7  0x01da3070 in -[UITransitionView _didCompleteTransition:] ()
> #8  0x01da531b in -[UITransitionView _transitionDidStop:finished:] ()
> #9  0x01d23fb6 in -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] ()
> #10 0x01d24154 in -[UIViewAnimationState animationDidStop:finished:] ()
> #11 0x0163bbce in CA::Layer::run_animation_callbacks ()
> #12 0x03664fe4 in _dispatch_client_callout ()
> #13 0x03655997 in _dispatch_main_queue_callback_4CF ()
> #14 0x012c03b5 in __CFRunLoopRun ()
> #15 0x012bf804 in CFRunLoopRunSpecific ()
> #16 0x012bf6db in CFRunLoopRunInMode ()
> #17 0x030f1913 in GSEventRunModal ()
> #18 0x030f1798 in GSEventRun ()
> #19 0x01ce82c1 in UIApplicationMain ()

**

根据苹果为 ios 5 更新的文档,他们提到:

presentModalViewController:动画:

向用户呈现由给定视图控制器管理的模态视图。(已弃用。presentViewController:animated:completion:改为使用。)

- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
Parameters

关闭接收者呈现的视图控制器。(已弃用。dismissViewControllerAnimated:completion:改为使用。)

- (void)dismissModalViewControllerAnimated:(BOOL)animated

我也试过这个,但它仍然崩溃

4

7 回答 7

5

您需要在您的班级中保留对 MFMailComposeViewController *mailer 的强引用,并且在您关闭它之后,您可以将该引用设为空。问我怎么知道的:-)

@implememtation MyClass
{
    MFMailComposeViewController *mailer;
}
...

(IBAction)FnForPlutoSupportEmailButtonPressed:(id)sender {
{
    if ([MFMailComposeViewController canSendMail])
    {
        /* USE IVAR */mailer = [[MFMailComposeViewController alloc] init];

稍后,当完全使用它时,您只需“mailer = nil;” 释放它。

编辑:我所做的和建议是在主队列中使用一个块来进行发布。如果你只是使用'self.mailer = nil',那么释放会在最终委托方法完成之后发生,你肯定不再使用它。

EDIT2:这种崩溃并非总是在所有设备上发生 - 我会说它在您收到最终委托方法和完成工作之间有点竞争条件。Apple 没有说明以一种或另一种方式保存引用 - 但是,Apple 产品的一般做法是假设您获得的任何对象都是通过一个 runLoop “借出”的,此后如果您想保留引用,您必须保留对象。

于 2012-08-08T17:22:43.893 回答
3

如果您在代码中实现了 sharekit 框架,请转到 SHK.m 并更改

        [[currentView parentViewController] dismissModalViewControllerAnimated:YES];

        [currentView  dismissModalViewControllerAnimated:YES];

这将解决您的问题。

谢谢大家的回复。

并评论这些行

SHKSwizzle([MFMailComposeViewController class], @selector(viewDidDisappear:), @selector(SHKviewDidDisappear:));
if (NSClassFromString(@"MFMessageComposeViewController") != nil) SHKSwizzle([MFMessageComposeViewController class], @selector(viewDidDisappear:), @selector(SHKviewDidDisappear:));
于 2012-08-23T06:15:48.003 回答
2

嗨,您可以使用以下代码发送邮件邮件应用程序不会发送任何电子邮件表单模拟器,您必须在设备上安装您的应用程序。当您尝试访问它们不在内存中的对象时会出现 EXE_BAD_ACCESS,并且 remamber 设备区分大小写。

-(void)sendMail:(id)sender
{
    //create instance of class at runtime.
    Class mailComposer = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil)
{
        // check the current device is configured for sending emails
        //if it is not configured for mail you open mail application on device.
    if ([mailClass canSendMail])
    {
        [self displayComposerSheet];
    }
    else
    {
        [self launchMailAppOnDevice];
    }
}
else
{
    [self launchMailAppOnDevice];
}

}
-(void)displayComposerSheet 
 {
MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@"Welcome!"];

    //recipients

NSArray *to = [NSArray arrayWithObject:@"jhon@example.com"];  

[picker setToRecipients:toRecipients];

    // Attach an image
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png"];
NSData *imageData = [NSData dataWithContentsOfFile:filePath];
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"Default"];

    // Set email body text
    //NSString *emailBody = @"......!";
    //[picker setMessageBody:emailBody isHTML:NO];

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


- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:   (MFMailComposeResult)result error:(NSError*)error 
{   
message.hidden = NO;
    // Notifies users about errors associated with the interface
switch (result)
{

    case MFMailComposeResultCancelled:
        message.text = @"Canceled";
        break;
    case MFMailComposeResultSaved:
        message.text = @"Saved";
        break;
    case MFMailComposeResultSent:
        message.text = @"Sent";
        break;
    case MFMailComposeResultFailed:
        message.text = @"Failed";
        break;
    default:
        message.text = @"Not sent";
        break;
}
[self dismissModalViewControllerAnimated:YES];
}

-(void)launchMailAppOnDevice
{
NSString *recipients = @"";
NSString *body = @"";

NSString *email1 = [NSString stringWithFormat:@"%@%@", recipients, body];
email1 = [email1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email1]];
}
于 2012-08-08T20:29:03.857 回答
2

由于堆栈的停止是0x00000000,我假设 UIKit 中的一些代码正在跳转到一个 nil 函数指针。启用 Zombies 并查看是否收到僵尸对象访问错误。(编辑您的项目方案,在运行/调试,诊断选项卡下,选中“启用僵尸对象”)

于 2012-08-13T07:35:38.873 回答
1

试试这个代码,这很好用,不会崩溃..联系我们方法是按钮目标选择器..

    -(void)ContactUs:(UIButton*)button
    {
        Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
        if (mailClass != nil)
        {
            // We must always check whether the current device is configured for sending emails
            if ([mailClass canSendMail])
            {
                [self displayComposerSheet];
            }
            else
            {
                [self launchMailAppOnDevice];
            }
        }
        else
        {
            [self launchMailAppOnDevice];
        }

    }

-(void)displayComposerSheet 
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    //[picker setSubject:@"Hello from California!"];

    // Set up recipients

    NSArray *toRecipients = [NSArray arrayWithObject:@"info@imp.co.in"]; 
    //NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil]; 
    //NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"]; 

    [picker setToRecipients:toRecipients];
    //[picker setCcRecipients:ccRecipients];    
    //[picker setBccRecipients:bccRecipients];

    // Attach an image to the email
    /*NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"];
     NSData *myData = [NSData dataWithContentsOfFile:path];
     [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"];*/

    // Fill out the email body text
    //NSString *emailBody = @"It is raining in sunny California!";
    //[picker setMessageBody:emailBody isHTML:NO];

    [self presentModalViewController:picker animated:YES];
    [picker release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{   
    message.hidden = NO;
    // Notifies users about errors associated with the interface
    switch (result)
    {

        case MFMailComposeResultCancelled:
            message.text = @"Canceled";
            break;
        case MFMailComposeResultSaved:
            message.text = @"Saved";
            break;
        case MFMailComposeResultSent:
            message.text = @"Sent";
            break;
        case MFMailComposeResultFailed:
            message.text = @"Failed";
            break;
        default:
            message.text = @"Not sent";
            break;
    }
    [self dismissModalViewControllerAnimated:YES];
}

-(void)launchMailAppOnDevice
{
    NSString *recipients = @"";
    NSString *body = @"";

    NSString *email1 = [NSString stringWithFormat:@"%@%@", recipients, body];
    email1 = [email1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email1]];
}
于 2012-07-31T09:04:45.927 回答
1

如果您使用 ARC,控制器对象将在您调用解除之前被释放。

将对象保留在堆栈分配变量之外的某个位置。

于 2012-08-09T03:39:11.250 回答
0

您是否在 .h 文件中设置了委托 MFMailComposeViewControllerDelegate。我过去曾遇到过这样的问题。而且错误非常小。请检查 .h 文件中的委托。

于 2012-08-13T11:11:22.470 回答