Question

I am implementing an iphone application (iOS 4.2) from where I would like to trigger the email client to send messages with attachments. I could effectively use uri schemes in combination with the class NSURL in order to trigger the email app but I was wondering whether it is possible to attach images. I have tried with mailto:whoever@wherever.org?subject=sthg&body=sthgelse&attachment=/path/to/file but the attachments are not included. I know iphone applications are sandboxed therefore it is possible that the email utility were not able to access the path to my image since it is located in my application bundle. On the other hand I was considering to administer my images with the photo manager. (1) Is there a way to include attachments in this way? (2) If so, is it possible to reference images either from my app or from the photo client? I could not find any attachments argument in the mailto RFC but maybe Apple has provided some way to achieve this.

Thanks in advance for your help,

Luis

Was it helpful?

Solution

MFMailComposeViewController will be able to do that, some example of usage belows: remember to add MessageUI.framework

MFMailComposeViewController *email = [[MFMailComposeViewController alloc] init];
email.mailComposeDelegate = self;
[email setSubject:@"Whatever"];

// Set up recipients
NSArray recipients = [NSArray arrayWithObject:@"whoever@wherever.org"]; 

[email setToRecipients:recipients];


// Attach an image to the email
UIImage *attachment = ...;
NSData *data = UIImagePNGRepresentation(attachment);
[email addAttachmentData:myData mimeType:@"image/png" fileName:@"ok.png"];

// Fill out the email body text
NSString *emailBody = @"test mail";
[email setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];

[email release];

OTHER TIPS

Instead of using the mailto: URL scheme, you should use the MFMailComposeViewController which allows you to add attachments. It also has the added benefit that using will not leave your app.

If one does not have account MFMailComposeViewController simply crashes. Yes, you can call canSendMail first with the result NO(!), what next? The answer is - use 'mailto:'. It'll popup dialog to create account.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top