如何消除MFMessageComposeViewController的显示延迟?

时间:2012-07-18 16:32:23

标签: iphone messageui

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

当执行此操作时,在实际显示撰写视图之前会有几秒钟的延迟。造成这种情况的原因是什么以及如何消除延迟?

编辑1:澄清:制作sms_message_vc和ivar无济于事,因为...alloc] init]进程会将UI挂起几秒钟,无论它在何处。

编辑2:尝试GCD(具有不同的优先级)以尝试同时运行初始化。没有帮助:

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

2 个答案:

答案 0 :(得分:0)

考虑将MFMessageComposeViewController * sms_message_vc设为类实例变量并调用:

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

之前,在启动self后立即将代理设置为sms_message_vc

然后就这样做:

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

当您想要实际发送消息时。这不应该改变太多,但可能有所帮助。

答案 1 :(得分:0)

我有同样的问题。 我试图将控制器缓存在一个静态变量中。但它没有用。行为不规律。第一次工作,第二次委托自动调用,没有任何用户操作,第三次屏幕变黑。看起来你必须在每次解雇后创建实例!

导入基金会 导入UIKit 导入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
}

}

用法,

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