Question

Once the sms window pops up, I cannot get it to close again. The cancel button does not work, and the send button will send the sms but not close the window.

I've looked through many of the similar questions on here and they all suggest either implementing the messageComposeViewController method or setting the messageComposeDelegate to self but ive done both of those things and still nothing.

Can anybody tell me what I'm doing wrong?

.h file

#import <MessageUI/MFMessageComposeViewController.h>
#import <MessageUI/MessageUI.h>
@interface ViewController : UIViewController<CLLocationManagerDelegate,MFMessageComposeViewControllerDelegate,UINavigationControllerDelegate>{
  CLLocationManager *locationManager;
  MKMapView *mapView_;

}

.m file

- (IBAction)SendTextTapped:(id)sender{
    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
    if([MFMessageComposeViewController canSendText])
    {
        controller.body = @"Hello!";
        controller.recipients = [NSArray arrayWithObjects:@"123456", nil];
        controller.messageComposeDelegate = self;
        [self presentViewController:controller animated:YES completion:nil];
    }
}


- (void)messageComposeViewController:(MFMessageComposeViewController*) controller didFinishWithResult:(MessageComposeResult)result error:(NSError*)error;
{
  NSLog(@"Entered messageComposeController");
  switch (result) {
    case MessageComposeResultSent: NSLog(@"SENT"); [self dismissViewControllerAnimated:YES completion:nil]; break;
    case MessageComposeResultFailed: NSLog(@"FAILED"); [self dismissViewControllerAnimated:YES completion:nil]; break;
    case MessageComposeResultCancelled: NSLog(@"CANCELLED"); [self dismissViewControllerAnimated:YES completion:nil]; break;
  }
}
Was it helpful?

Solution

Remove the error parameter on your delegate method. The delegate method should be:

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    // code here
}

NOT:

- (void)messageComposeViewController:(MFMessageComposeViewController*) controller didFinishWithResult:(MessageComposeResult)result error:(NSError*)error
{
    // code here
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top