asedra_le
asedra_le

Reputation: 3067

Can't set recipients of MFMessageComposeViewController?

I have a method like this:

void sendSMS{

        if ([MFMessageComposeViewController canSendText]) {

            MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];

            picker.recipients = [NSArray arrayWithObject:@"0933660805"];
            [picker  setBody:@"Message body"];
            picker.messageComposeDelegate = self;

            [self.navigationController  presentModalViewController:picker animated:YES];
            //[picker release];

            return;

        }

    }

Message composer open but recipients and message body are empty (image below). Anybody know how can i fix it :(

enter image description here

Upvotes: 1

Views: 5140

Answers (8)

M. Porooshani
M. Porooshani

Reputation: 1837

iOS 10.0 is here and this is still a problem for me. So, I have fashioned a workaround. According to previous comments that initializing the MFMessageComposeViewController in the viewDidLoad() won't solve the problem (which I can attest to), unless the view controller gets presented, it won't be cached. So, the hack here is to make a window, set its root view controller, present a dummy MFMessageComposeViewController instance and immediately dismiss it, somewhere before your actual need (like in viewDidLoad()) Here is a sample code I'm using (Swift 3.0 - Let me know if you were interested in Obj-C counterpart):

let window = UIWindow()
let vc = UIViewController()
window.rootViewController = vc
let messageCompose = MFMessageComposeViewController()
vc.present(messageCompose, animated: false) { [weak messageCompose] in
  messageCompose?.dismiss(animated: false, completion: nil)
}

The thing here is that if you present it in the currently active window's view controller chain, it will mess up your UI by showing and hiding the keyboard abruptly (no matter how you try to hide the controller's view and what not), due to the message body selection on present. But adding it to a whole new window which is not in view cycle, it will be correctly initialized and there will be no trace of such transaction on view. Plus, you won't boggle the memory too much this way (because the scope of the controller should be minimal now) and you can initialize your actual MFMessageComposeViewController any time you want and get it much faster now. If your application heavily relies on MFMessageComposeViewController (which I doubt) you can move this to your AppDelegate to be ready anywhere around your app's life cycle. Cheers, M.

Upvotes: 1

Russ Hooper
Russ Hooper

Reputation: 441

You should have a "nil" at the end of the array:

composeViewController.recipients = [NSArray arrayWithObject:@"0933660805", nil];

Upvotes: 0

Jahm
Jahm

Reputation: 668

Try this.

- (void)forwardPromo
{
    MFMessageComposeViewController *composeViewController = [[MFMessageComposeViewController alloc] init];
    composeViewController.body = @"Message body";
    composeViewController.recipients = [NSArray arrayWithObject:@"0933660805"];
    composeViewController.messageComposeDelegate = self;

    [self presentViewController:composeViewController animated:YES completion:nil];
}

Upvotes: 0

senseMe
senseMe

Reputation: 1

set the MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];

before if ([MFMessageComposeViewController canSendText]) {...}

Try this.

Upvotes: 0

BergP
BergP

Reputation: 3545

In my case (on iPhone 3g s) the problem was when I called [self.navigationController pushViewController... ], when i tried call [self presentModalViewController ...] it worked, I dont know why, but it is. Try it.

Upvotes: 0

Joe C
Joe C

Reputation: 2834

OK I answered my own question. Now I want no one else to have to go thru this. I was calling this method from just an NSObject. It was a delegate to MFMessageComposeViewControllerDelegate but that made no difference. I had to move this method to my MainViewController, then it worked.

Upvotes: 2

Sudhanshu
Sudhanshu

Reputation: 3960

Go for this ones and then check may be it will resolve your issue

void sendSMS
{
    if ([MFMessageComposeViewController canSendText]) {
        MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
        picker.messageComposeDelegate = self;
        NSString *bodyString = nil;

        NSMutableArray *toRecipients = [[NSMutableArray alloc]init];
        [toRecipients addObject:@"0933660805"];
        [picker setRecipients:(NSArray *)toRecipients];
        [toRecipients release];

        bodyString = [NSString stringWithFormat: @"Message body"];
        [picker setBody:bodyString];

        [self presentModalViewController:picker animated:YES];
        [picker release];
}

Also take a look at this tutorial http://blog.mugunthkumar.com/coding/iphone-tutorial-how-to-send-in-app-sms/

Good Luck!

Upvotes: 3

visakh7
visakh7

Reputation: 26400

Try this

- (void)sendSMS
{

if ([MFMessageComposeViewController canSendText]) {
        MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
        picker.messageComposeDelegate = self;
        NSString *bodyString = nil;

        NSArray *toRecipients = [NSArray arrayWithObject:@"NUMBER HERE"]; 
    [picker setRecipients:toRecipients];

        [self presentModalViewController:picker animated:YES];
        [picker release];

}

Upvotes: 0

Related Questions