简体   繁体   中英

After hitting cancel in a MFMessageComposeViewController nothing happens

After though that after using MFMailComposeViewController the move to MFMessageComposeViewController was straight foward, but there is a catch.

Suppose this code:

MFMessageComposeViewController* mySMS = [[MFMessageComposeViewController alloc] init];
[mySMS setDelegate:self];
[self presentModalViewController:mySMS animated:YES];

It works this way for mails, but in sms you should set different the delegate to an internal structure like this:

[SMS setMessageComposeDelegate:self];

Hope you don not get stuck on this as I did early today.

You need to Implement the delegate method -(void)mailComposeController(MFMailComposeViewController*)controller didFinishWithResult (MFMailComposeResult)result error:(NSError*)error:

And inside it you should dismiss it yourself:

-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{
    [self dismissModalViewControllerAnimated:YES]; 
}

You can see MFMailComposeResult in Apple documentation

  enum MFMailComposeResult {
    MFMailComposeResultCancelled,
    MFMailComposeResultSaved,
    MFMailComposeResultSent,
    MFMailComposeResultFailed
 };
 typedef enum MFMailComposeResult MFMailComposeResult; 

And you must dismiss controller by yourself in delegate method

 - (void) mailComposeController:(MFMailComposeViewController *)controller          didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
 {
  switch (result){
    case MFMailComposeResultCancelled:
        NSLog(@"Mail cancelled");
        break;
    case MFMailComposeResultSaved:
        NSLog(@"Mail saved");
        break;
    case MFMailComposeResultSent:
        NSLog(@"Mail sent");
        break;
    case MFMailComposeResultFailed:
        NSLog(@"Mail sent failure: %@", [error localizedDescription]);
        break;
    default:
        break;
   }
// Close the Mail Interface
   [self dismissViewControllerAnimated:YES completion:NULL];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM