Search code examples
iosxcodensstringviewcontrollermfmessagecomposeviewcontroller

MFMessageComposeViewController not working


I have a simple method that gets one argument and then sends a message. It is not working.

Code:

- (void)sendSMS:(NSString *)text {
    MFMessageComposeViewController *viewController = [[MFMessageComposeViewController alloc] init];
    viewController.body = text;
    viewControllerM.mailComposeDelegate = self;
    [self presentViewController:viewController animated:YES completion:nil];
}

What's wrong?


Solution

  • You need to set the delegate:

    MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
        if([MFMessageComposeViewController canSendText])
        {
            controller.body = @"YO";
            controller.recipients = [NSArray arrayWithObjects:@"5625555555", nil];
            controller.messageComposeDelegate = self;
    
            [self presentViewController:controller animated:YES completion:nil];
        }