How to eliminate delay in presentation of MFMessageComposeViewController?

梦想的初衷 提交于 2019-12-23 10:18:04

问题


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

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/11546109/how-to-eliminate-delay-in-presentation-of-mfmessagecomposeviewcontroller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!