MFMessageComposeViewController取消按钮不起作用

时间:2015-06-25 07:43:41

标签: ios objective-c mfmailcomposeviewcontroller

我使用此块向联系人发送消息,发送后,后退按钮就在那里,但是当我触摸它时没有任何反应。请帮帮我:))

-(IBAction) InviteIt:(id) sender{


    if ([MFMessageComposeViewController canSendText]) {
        MFMessageComposeViewController *messageComposer =
        [[MFMessageComposeViewController alloc] init];
        messageComposer.messageComposeDelegate = self;
        NSString *message = @"You have more body buddies than you think at: http://www.itunes.com/app/JoyChain ";
        [messageComposer setBody:message];
        messageComposer.recipients = [NSArray arrayWithObjects:_itsnum, nil];
        messageComposer.messageComposeDelegate = self;
        [self presentViewController:messageComposer animated:YES completion:nil];

    }

}

6 个答案:

答案 0 :(得分:18)

您是否忘记实施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];
}

来自苹果文档:

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

但你可以根据你的目标处理所有情况......

答案 1 :(得分:2)

正确答案是设置控制器messageComposeDelegate,并定义以下委托方法:

- (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的回答基于MFMailComposeViewController而不是MFMessageComposeViewController

答案 2 :(得分:2)

这是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)
    }

答案 3 :(得分:1)

如果有人需要它,则将messageComposeDelegate方法转换为Swift(3):

func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
    controller.dismiss(animated: true)
}

答案 4 :(得分:0)

以下是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;
        }

答案 5 :(得分:0)

就像答案一样,您需要在代表中解雇。我的问题是我设置的是delegate而不是messageComposeDelegate(对于电子邮件版本,则设置为mailComposeDelegate)。

确保您设置了正确的代表:)