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

执行此操作时,在实际显示撰写视图之前会有几秒钟的延迟。是什么导致了这种情况,如何消除延迟?

编辑 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; 
});
4

2 回答 2

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

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

于 2012-07-18T16:42:27.887 回答
0

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

导入 Foundation 导入 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)")
        }
    }
于 2018-02-24T22:00:46.873 回答