开发者

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  开发者_Python百科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 :(

Can't set recipients of MFMessageComposeViewController?


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!


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.


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.


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];

}


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.


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

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

Try this.


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];
}


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜