views:

397

answers:

3

Has anyone every embeded the iPhone's Email program inside your own App?

Let me try and simplify that. Tap Tap Revenge allows you to "Challenge A Friend". When you choose to do so they open the standard iPhone email program (if they mimicked it, it looks damn good), within the application with pre-populated data. All you have to do is select a friend from your contacts and press send. You never leave the Tap Tap Revenge App.

Any ideas how this is done?

+4  A: 

You need to include the MessageUI.framework into your project, and inside your header file you need to set the delegate:

#import <MessageUI/MessageUI.h>
@interface RootViewController : UIViewController <MFMailComposeViewControllerDelegate> {
    MFMailComposeViewController *email;
}

@property (nonatomic, retain) MFMailComposeViewController *email;

Once you do that, you have a few delegate methods inside your implementation file you need to include (You should check to see the result, but I am trying to keep as little code as needed):

@synthesize email;

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    [email dismissModalViewControllerAnimated:YES];
}

Wherever you want to use this, you need to initialize and set it up like this:

email = [[MFMailComposeViewController alloc] init];
email.mailComposeDelegate = self;

// Subject
[email setSubject:@"Testing"];

// Optional Attachments
NSData *artwork = UIImagePNGRepresentation([UIImage imageNamed:@"albumart.png"]);
[email addAttachmentData:artwork mimeType:@"image/png" fileName:@"albumart.png"];

// Body
[email setMessageBody:@"This is the body"];

// Present it
[self presentModalViewController:email animated:YES];
Garrett
Thank you, this will point me in the right direction and get me started! Many thanks.
bbullis21
A: 

There is two answers for that. For applications that should support iPhone OS prior 3.0 the only way to go is to build a custom message composer. Or you may want to have a look at the component built by Joe Hewitt who wrote the Facebook iPhone app.

http://github.com/joehewitt/three20/blob/master/src/Three20/TTMessageController.h

With iPhone SDK 3.0 you are able to use the Mail message composer UI straight out if the box using the MessageUI.framework as explained above.

ck
Thanks for the link.
bbullis21
+2  A: 

Further to Garett's great response, should you get the warning:

'MFMailComposeViewController' may not respond to '-setMessageBody:'

add isHTML: so the complete line reads like:

[mail setMessageBody:@"This is the body" isHTML:NO];