MFMailComposeViewController在出现时立即消失

时间:2016-04-12 11:44:26

标签: ios objective-c xcode7 mfmailcomposeviewcontroller

MFMailComposeViewController在出现时立即解除

- (IBAction)btnContactPressed:(id)sender
{
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
        mailer.mailComposeDelegate = self;
        [mailer setSubject:@"Feedback"];

        NSArray *toRecipients = [NSArray arrayWithObjects:@"salimullah240@gmail.com", nil];
        [mailer setToRecipients:toRecipients];

        [self presentViewController:mailer animated:YES completion:nil];
    } 
    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
{
    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 dismissViewControllerAnimated:YES completion:nil];
}

1 个答案:

答案 0 :(得分:0)

您的代码是正确的。我试过并且工作得很好。 MFMailComposeViewController组件无法仅在iOS simulator中的device中进行测试。

如果您在Apple Developer Forums中查看此Thread,问题就会在Apple Bug Report中显示,但仍然没有任何解决方法。

另外,请确保您要导入:

#import <MessageUI/MFMailComposeViewController.h> 

并添加delegate

@interface ViewController () <MFMailComposeViewControllerDelegate>
相关问题