views:

832

answers:

4

My iPhone app is using the MFMailComposeViewController class to send an in-app email with an attachment. The app will only attempt to display the mail composer dialog if the "canSendMail" method of class MFMailComposeViewController returns true (YES). Specifically, if the following method returns YES, it shows the mail composer, otherwise the user is presented with an error alert dialog stating that there are no email accounts set up on the device:

- (BOOL)canDeviceSendEmail
{
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    return mailClass != nil && [mailClass canSendMail];
}

A team of testers have reported that they get this error alert dialog, even when email accounts are set up on the device. The tester used an iPhone 3G with OS 3.1.3. Therefore the MFMailComposeViewController class must have existed, and the "canSendMail" method must have returned NO.

My question is therefore: apart from the case when there are no email accounts set up on the device, in what other circumstances can the "canSendMail" method return NO?

~ Thanks

A: 

You can't be certain that MFMailComposeViewController must have existed because your method doesn't discriminate between MFMailComposeViewController not existing and [MFMailComposeViewController canSendMail] returning NO.

What iPhone OS you're testing on is not relevant; what version of the iPhone SDK your application links against is what determines whether MFMailComposeViewController will be available at runtime. If you want that class, you need to build with SDK 3.0 or later.

Tom
Thanks for that. The app was built with base SDK 3.1.2 and the iPhone OS Deployment Target is iPhone OS 3.0
pythonquick
Aha. This technique of setting the iPhone OS Deployment Target separate from the Base SDK and checking at runtime for classes is new to me. Pretty cool.
Tom
+1  A: 

For +canSendMail to return YES the default account must be set-up for sending emails.

(Internally, +[MFMailComposeViewController canSendMail] calls +[MailAccountProxy defaultMailAccountForDelivery], which finds the first mail account being -isDefaultDeliveryAccount.)

KennyTM
Ah, interesting point. I tried removing the (outgoing) SMTP server from the email account. It opened the email composer window, and the email stayed in the account's Outbox, so +[MFMailComposeViewController canSendMail] still returned YES. The only way to make it return NO, was when all email accounts were either disabled or completely removed. Is there another way an account would not be able to send emails, so that canSendMail returns NO?
pythonquick
A: 

Did you include the Messaging Framework? I had a similar issue once and it was only because I forgot to add the correct framework to the XCode project.

frenetisch applaudierend
Yes, the MessageUI.framework is included. On my own test device the emailing does work, as long as there's at least one email account, that's enabled. So the MessageUI.framework must be included correctly.
pythonquick
+1  A: 

If at least one email account is enabled on the device, the following call should return YES:

[MFMailComposeViewController canSendMail]

Conversely, if all accounts are disabled/removed, it will return NO.

byneri