Search code examples
iosmultithreadingxamarin.iosmfmailcomposeviewcontrollermfmessagecomposeviewcontroller

MonoTouch: consecutive views


I'm currently trying to display the email view (MFMailComposeViewController) after the SMS one (MFMessageComposeViewController), after the user send the message or cancel..

The MFMessageComposeViewController is closed correctly, but the MFMailComposeViewController doesn't shows up.

I searched on threads, InvokeInMainThread, but it doesn't show or it is not in main thread. Honestly, I don't know at all threading, and documentation I read is not clear.

Method to display MFMessageComposeViewController:

private void SendSms ()
{
    string strMessage = MessageComposer.GetMessage(isEmergency, latitude, longitude, location);
    MFMessageComposeViewController message = Sender.SendSms(strMessage, AppDelegate.contacts);
        message.Finished += delegate {
        message.DismissModalViewControllerAnimated(true);
    };
    this.NavigationController.PresentModalViewController (message, true);
}

The MFMailComposeViewController one is not very different. I call this method that way: InvokeOnMainThread(delegate { SendSms(); }); and works perfectly.

My bad is when this closed. I tried ThreadPool.QueueUserWorkItem(new WaitCallback(new Object(), SendEmail)); but it's not in the main thread.

I tried that one:

SendDelegate d = new SendDelegate(SendSms);
AsyncCallback callBack = new AsyncCallback(SendEmail);
IAsyncResult ar = d.BeginInvoke(new Object(), callBack, null);

Not in the main thread.

Edit: solution code

Here is how it was solved ! I should have thought about that...

MFMessageComposeViewController message = new MFMessageComposeViewController();
message.Finished += delegate {
    message.DismissModalViewControllerAnimated(true);
        ThreadPool.QueueUserWorkItem ((e) => {
                        Thread.Sleep(500);
                        InvokeOnMainThread (delegate { 
                SendEmail(); 
                        });
                });
    };
    this.NavigationController.PresentModalViewController (message, true);
}

Solution

  • You cannot make a second call to PresentModalViewController while another call to DismissModalViewController is in progress. (There is probably an error presented to the console if you try to do so.)

    So your options are:

    • Use a timer to show the second modal view controller
    • Use a different approach to your UI that makes more sense

    I would recommend the second option. Maybe use two different buttons or menu options for the two operations.