MFMessageComposeViewController cancel button not working

前端 未结 6 490
慢半拍i
慢半拍i 2021-01-17 10:42

I use this block to send message to the contacts, after sending, the back button is there, but when I touch it nothing happens. Please help me out :)

-(IBAct         


        
相关标签:
6条回答
  • 2021-01-17 11:23

    Did you forget to implement mailComposeController:didFinishWithResult: ?...

    - (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
        [controller dismissViewControllerAnimated:YES completion:nil];
    }
    

    From apple documentation:

    // The mail compose view controller delegate method
    - (void)mailComposeController:(MFMailComposeViewController *)controller
                  didFinishWithResult:(MFMailComposeResult)result
                  error:(NSError *)error
    {
        [self dismissModalViewControllerAnimated:YES];
    }
    

    But you can handle all cases depend on your goal ...

    0 讨论(0)
  • 2021-01-17 11:24

    Here's the answer for Swift 4, iOS 11

    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
            print("Emailing attempt, error="+(error?.localizedDescription)!)        
            switch (result){
            case MFMailComposeResult.cancelled:
                print("Mail cancelled");
                break;
            case MFMailComposeResult.saved:
                print("Mail saved");
                break;
            case MFMailComposeResult.sent:
                print("Mail sent");
                break;
            case MFMailComposeResult.failed:
                print("Mail sent failure: %@", error?.localizedDescription);
                break;
            default:
                break;
            }
            // Close the Mail Interface
            controller.dismiss(animated: true)
        }
    
    0 讨论(0)
  • 2021-01-17 11:24

    If anyone needs it, the messageComposeDelegate method translated into Swift (3):

    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        controller.dismiss(animated: true)
    }
    
    0 讨论(0)
  • 2021-01-17 11:25

    Here's the answer for Swift 4, iOS 11

    case MFMailComposeResult.cancelled:
                print("Mail cancelled");
                break;
            case MFMailComposeResult.saved:
                print("Mail saved");
                break;
            case MFMailComposeResult.sent:
                print("Mail sent");
                break;
            case MFMailComposeResult.failed:
                print("Mail sent failure: %@", error?.localizedDescription);
                break;
            default:
                break;
            }
    
    0 讨论(0)
  • 2021-01-17 11:38

    The correct answer is to set the controller's messageComposeDelegate, and to define the following delegate method:

    - (void)messageComposeViewController:(MFMessageComposeViewCont‌​roller *)controller
                     didFinishWithResult:(MessageComposeResult)result {
    
      [self dismissModalViewControllerAnimated:YES];
      if (result == MessageComposeResultCancelled)
        NSLog(@"Message cancelled");
      else if (result == MessageComposeResultSent)
        NSLog(@"Message sent");
      else
        NSLog(@"Message failed");
    }
    

    @Doro's answer is based on the MFMailComposeViewController not the MFMessageComposeViewController

    0 讨论(0)
  • 2021-01-17 11:38

    Like the answers say, you need to dismiss in the delegate. My issue was I was setting delegate instead of messageComposeDelegate (or mailComposeDelegate for the e-mail version).

    Be sure you're setting the right delegate :)

    0 讨论(0)
提交回复
热议问题