Search code examples
c#iosxamarin.iosmfmailcomposeviewcontrollermessageui

NSInvalidArgumentException while Presenting ModalView


Exception Message:

Objective-C exception thrown.  Name: NSInvalidArgumentException Reason: 
Application tried to present a nil modal view controller on target <Navigator: 0x1bed0d0>.

Here is my code:

    partial void BtnTest (MonoTouch.Foundation.NSObject sender)
    {
        MFMailComposeViewController view = new MFMailComposeViewController();
        view.SetToRecipients(new string[]{"[email protected]"});
        view.SetMessageBody("Hier steht nun der zusammengestellt text :)", false);
        //view.MailComposeDelegate = new CustomMailComposeDelegate();
        view.SetSubject("Test");

        view.Finished += (s,e)=>
                     {
                            this.NavigationController.DismissModalViewControllerAnimated(true);
        };

        this.BeginInvokeOnMainThread(()=>
        {
            this.NavigationController.PresentModalViewController(view, true);
        });

    }

It works on iPad Emulator but not on the Device.


Solution

  • Is your device configured to send emails ? note that even if it is you should not consider it will be the case on every user devices.

    IWO you should call MFMailComposeViewController.CanSendMail like this is documented by Apple. Two important quotes:

    you must always check to see if the current device is configured to send email at all using the canSendMail method

    and

    You should not attempt to use this interface if the canSendMail method returns NO.

    Example:

       if (MFMailComposeViewController.CanSendMail) {
           ... your code ...
       } else {
           ... show warning, like an UIAlertView
       }