Forum Xamarin.iOS

Need help to handle e-mail, when device's email account not setup on iOS

Whitchurch.6690Whitchurch.6690 USMember ✭✭
edited September 2016 in Xamarin.iOS

I have written some code, to open the the email app on the phone(device) and auto populate, the e-mail with the passed in parameter.

  • Everything works fine, if I have setup the e-mail account as expected.
  • If I do not have the e-mail account setup the app crashes.

  • I have written the following code hoping that a return at CanSendMail would prevent the crash, but it is not helping:

Device.BeginInovkeOnMainThread(()=>{
if(!MFMailComposeViewController.CanSendMail)
{
System.Diagnostics.Debug.WriteLine("Unable to send mail"); // Still crashes at the return.
return ;
}
else
{
var _mailController = new MFMailComposeViewController();

                _mailController.SetToRecipients(new[] { "[email protected]" });
                _mailController.SetSubject("E-mail Subject");
                _mailController.SetMessageBody("Blah blah blah", false);


                // Handle the action to take when the user completes sending the email
                _mailController.Finished += (object s, MFComposeResultEventArgs args) => {
                    System.Console.WriteLine(args.Result.ToString());
                    args.Controller.DismissViewController(true, null);
                };

                UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(_mailController, true, null);
            }

});

Best Answer

Answers

Sign In or Register to comment.