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;
});
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.
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)")
}
}