0

i need to send multiple text messages , raising the application message several times. But the console show this error:

2016-08-27 19:27:17.237 AlertaTel 2.0[841:263754] Attempt to present <MFMessageComposeViewController: 0x15e19ba00> on <AlertaTel_2_0.ViewController: 0x15de43af0> which is waiting for a delayed presention of <MFMessageComposeViewController: 0x15e24ca00> to complete

I read on this site about this issue, but only found solutions or topics in Objective- c and honestly do not master the language even (I'm more oriented Swfit ).

I attached my codes:

Class MessageComposer

class MessageComposer: NSObject, MFMessageComposeViewControllerDelegate {

// A wrapper function to indicate whether or not a text message can be sent from the user's device
func canSendText() -> Bool {
    return MFMessageComposeViewController.canSendText()
}

// Configures and returns a MFMessageComposeViewController instance
func configuredMessageComposeViewController(unicaVariable : String) -> MFMessageComposeViewController {
    let messageComposeVC = MFMessageComposeViewController()
    messageComposeVC.messageComposeDelegate = self  //  Make sure to set this property to self, so that the controller can be dismissed!
    messageComposeVC.recipients = textMessageRecipients
    messageComposeVC.body = "Estoy en peligro, aca esta mi última ubicación: https://maps.google.com/maps?q="+(view.locationManager.location?.coordinate.latitude.description)!+","+(view.locationManager.location?.coordinate.longitude.description)!+". "+(unicaVariable)
    //view.performRequestAndUpdateUI()
    return messageComposeVC

}

// MFMessageComposeViewControllerDelegate callback - dismisses the view controller when the user is finished with it
func messageComposeViewController(controller: MFMessageComposeViewController, didFinishWithResult result: MessageComposeResult) {
    controller.dismissViewControllerAnimated(true, completion: nil)
}

}

In the ViewController:

 func levantarMensaje(datoWebService: String){
    if (messageComposer.canSendText()) {
        let messageComposeVC = messageComposer.configuredMessageComposeViewController(datoWebService)
        presentViewController(messageComposeVC, animated: true, completion: nil)

    } else {
        // Let the user know if his/her device isn't able to send text messages

    }
}

And i call this method in a @IBAction:

  @IBAction func sendTextMessageButtonTapped(sender: UIButton) {
    levantarMensaje()
}

When I implemented a simple " FOR" on the IBAction the error that I showed above appears.

Thank you very much for your answers , greetings !

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • The error complete is: 2016-08-27 19:27:17.237 AlertaTel 2.0[841:263754] Attempt to present on which is waiting for a delayed presention of to complete – Leonel Vaccaro Aug 27 '16 at 23:07

1 Answers1

2

What's happening here is that you're trying to begin a modal presentation while the previous modal presentation is still animating. UIKit doesn't like that; you need to wait until one presentation finishes before starting the next one. There are a couple of ways to do this.

The first is to have several modal presentations at the same time, but to make sure the animations don't happen simultaneously. You could do this by changing your call to presentViewController(_:, animated:, completion:) to use the completion argument to present the next message view controller. That way the first message view would appear, and when it was finished animating the next one would begin, etc.

The other would be to wait until one message is sent (or cancelled) before presenting the next one. For that you'd replace controller.dismissViewControllerAnimated(true, completion: nil) with something similar to what I described above. Instead of passing nil for the completion argument, pass a closure that presents the next message view, until none remain.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170