5
if([MFMessageComposeViewController canSendText])
{
    MFMessageComposeViewController *sms_message_vc = [[MFMessageComposeViewController alloc] init];
    sms_message_vc.body = text;
    sms_message_vc.recipients = recipients;
    sms_message_vc.messageComposeDelegate = self; 
    [self presentModalViewController:sms_message_vc animated:FALSE];
    [[UIApplication sharedApplication] setStatusBarHidden:TRUE];
    [sms_message_vc release];
}

When this executes there's a delay of several seconds before the compose view is actually shown. What is causing this and how does one go about eliminating the delay?

EDIT 1: Clarification: Making sms_message_vc and ivar doesn't help because the ...alloc] init] process will hang the UI for seconds, regardless of where it is.

EDIT 2: Tried GCD (with different priorities) to attempt to run initialization concurrently. Did not help:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, (unsigned long)NULL), ^(void){
    sms_message_vc = [[MFMessageComposeViewController alloc] init];
    sms_message_vc.messageComposeDelegate = self; 
});
martin's
  • 3,853
  • 6
  • 32
  • 58

2 Answers2

0

Consider making MFMessageComposeViewController *sms_message_vc a class instance variable and calling:

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

earlier, along with setting the delegate to self right after initing sms_message_vc

Then just do:

sms_message_vc.body = text;
sms_message_vc.recipients = recipients;
[self presentModalViewController:sms_message_vc animated:FALSE];
[[UIApplication sharedApplication] setStatusBarHidden:TRUE];
[sms_message_vc release];

When you want to actually send the message. This shouldn't change it too much but might help some.

Eric
  • 4,063
  • 2
  • 27
  • 49
  • Tried it. The problem with that approach is that when ...alloc] init] executes the phone just hangs waiting for something. So making it an ivar and initializing during viewDidLoad, as an example, causes your view to hang until the ...alloc] init] process takes place. – martin's Jul 18 '12 at 18:00
  • I guess all of this code has to be somewhere, perhaps shared in the App Delegate so it's called on launch? I'm not sure where else you can hide it. – Eric Jul 18 '12 at 18:03
  • I even tried to use GCD to see if ...alloc] init] could happen concurrently to no avail. See my second edit for details. – martin's Jul 18 '12 at 18:25
0

I have the same problem. I tried to cache the controller in a static variable. But it did not work. behaved erratically. First time works, second time delegate called automatically without any user action and 3rd time screen goes black. Looks like you have to create the instance after each dismiss!

import Foundation import UIKit import MessageUI

class UIUtil {

static var messageController:MFMessageComposeViewController? = nil
static var checkedOnce = false

class func createMessageController () -> MFMessageComposeViewController? {
    if checkedOnce {
        return messageController
    }
    checkedOnce = true
    if (MFMessageComposeViewController.canSendText()) {
        messageController = MFMessageComposeViewController()
        messageController?.recipients = [SettingsManager.shared.switchPhoneNumber]
    } else {
        print("SMS services are not available in this device.")
    }
    return messageController
}

}

usage,

func createSMSView (text:String) {
        print("Sending SMS to \(SettingsManager.shared.switchPhoneNumber). Text: \(text)")
        if let ctr = UIUtil.createMessageController() {
            ctr.body = text
            ctr.messageComposeDelegate = self
            self.present(ctr, animated: true, completion: nil)
        } else {
            print("Could not send SMS. Text: \(text)")
        }
    }
karim
  • 15,408
  • 7
  • 58
  • 96