34

I want to know if it's possible to send email through iPhone simulator. I have seen the tutorial for sending an email through iphone as below:

http://www.edumobile.org/iphone/iphone-programming-tutorials/compose-mail-application-in-iphone/

Now to test it is it necessary to have real device? What is the way if I want to send email through iPhone simulator?

Caleb
  • 120,112
  • 19
  • 171
  • 259
nehal
  • 614
  • 1
  • 13
  • 26

4 Answers4

34

You have to rely on the iOS that the MFMailComposeResult that is handed back in mailComposeController:didFinishWithResult:error: is correct. The simulator fakes that result; no actual mail is sent although it says MFMailComposeResultSent.

The tutorial mentioned misses an important point: The first thing you should do before using MFMailComposeViewController is to check [MFMailComposeViewController canSendMail]. That will return NO, if the user hasn't configured mail on their device. If you must support an iOS version prior to 3.0 the correct way is to check if the class MFMailComposeViewController exists:

Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil)
{
    if ([mailClass canSendMail])
    {
        [self displayComposerSheet];
    }
    else
    {
        [self launchMailAppOnDevice];
    }
}
else
{
    [self launchMailAppOnDevice];
}

The canSendMail-issue can only be tested on a real device though. It will crash if you don't check canSendMail and the user has no mail account configured.

Felix
  • 35,041
  • 12
  • 93
  • 141
  • For my task I just need to display the composer. But I am not able to do so. MFMailComposeViewController* composeVC = [[MFMailComposeViewController alloc] init]; . This line pops up the alert controller. – Ankit Maurya Dec 15 '17 at 07:53
7

As per the discussion on apple forum, to test the functionality we really need a device, simulator does not support this functionality.

A part from discussion:

sptrakesh Chicago Re: IOS SIMULATOR

MAIL APP Mar 26, 2012 7:09 AM (in response to davemac75)

The mail application is not available on the simulator. You will need to test your application on a device to test that part.

NeverHopeless
  • 10,503
  • 4
  • 33
  • 53
4

Yes it is necessary if you want to actually send the email.

in most of the cases there is no need for you to worry as the mail is going to be sent by apple app, so you will need only to verify that your app is responding and launches the mail composer. The only thing i can think as problematic is if you want to make sure that attachments and images are sent properly. In this case you can send a beta to someone with iphone and ask him to check it for you.

important having a device is critic to developing, your simulator does not behave exactly as the device. he is a forgiver and in my experience i always had issues with the device that weren't with the simulator.

Seyther
  • 642
  • 4
  • 6
shannoga
  • 19,000
  • 20
  • 99
  • 163
  • For my task I just need to display the composer. But I am not able to do so. MFMailComposeViewController* composeVC = [[MFMailComposeViewController alloc] init]; . This line pops up the alert controller – Ankit Maurya Dec 15 '17 at 07:53
1

You can use the MessageUI framework on the simulator to compose and 'send' messages, but I don't believe there's a way to actually send the message. Once the user hits the Send button in the message composition view, though, your code doesn't have any role in sending the message. So the simulator does enough that you can develop and test your app.

As for whether it's necessary to have a real device, I'd say that at some point you need to test on one or more devices no matter what. The simulator is a great tool, but after a certain point it's no substitute for running your app on the real thing.

Caleb
  • 120,112
  • 19
  • 171
  • 259