3

我有以下代码,当调用动作表上的按钮时被调用。但是当我按取消,然后删除草稿,它只是收费,并没有解雇。我在我的应用程序中的其他地方使用相同的代码,并从选择的tableview单元中调用它,并且它在那里找到。任何想法,为什么它不在这里工作?MFMailComposeViewController不从视图中消失

Ther在控制台冻结时也没有错误信息。

if([MFMailComposeViewController canSendMail]) 
{ 
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
    picker.mailComposeDelegate = self; 

    [picker setSubject:@"Dr. Chrono Support"]; 

    NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary]; 
    NSString* versionNum = [infoDict objectForKey:@"CFBundleVersion"]; 
    NSString *appName = [infoDict objectForKey:@"CFBundleDisplayName"]; 
    NSString *text = [NSString stringWithFormat:@"%@ %@",appName,versionNum]; 

    // Set up recipients 
    NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"]; 
    //NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]; 
    //NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"]; 

    [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 = text; 
    [picker setMessageBody:emailBody isHTML:NO]; 

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

回答

11

您需要实现的委托方法:

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { 

然后在此委托方法Add:

[self dismissModalViewControllerAnimated:YES]; 

,它应该工作得很好。

你不必去寻找结果(仅当你想显示“谢谢你”警报或东西,例如,如果用户确实击中发送)

+0

谢谢,我完全忘了补充一点,谢谢! – Jon

0

使用下面的代码

- (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 the next time the user connects to email"); 
      break; 
     case MFMailComposeResultFailed: 
      NSLog(@"Mail failed: the email message was nog saved or queued, possibly due to an error"); 
      break; 
     default: 
      NSLog(@"Mail not sent"); 
      break; 
    } 

    [self dismissModalViewControllerAnimated:YES]; 
}