Forum Navigation
You need to log in to create posts and topics.

Email from iPhone app

Posted by: fbcocoa.bw <fbcocoa.bw@...>

My first attempt at implementing email from an iPhone app looked something like:
NSString *to = @"[email protected]";
NSString *subject = @"Subject text";
NSString *body = @"Message";
NSString *mailString = [NSString stringWithFormat:@"mailto:?to=%@&amp;subject=%@&amp;body=%@",
[to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailString]];

It does the job but doing it this way forces the app to quit so that Mail app can run. Much better would be to display the standard email interface inside the app. Here's my second attempt:
[1] Add "MessageUI.framework" to the project
[2] Above @interface, insert:
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

[3] Add <MFMailComposeViewControllerDelegate> to the @interface line. (All this does is eliminate a harmless compiler warning):

@interface MailFromAppViewController : UIViewController <MFMailComposeViewControllerDelegate> {
// .....
}

[4] In @implementation: 
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)mailButtonPressed:(id)sender {
NSArray *recipients = [[NSArray alloc] initWithObject:@"[email protected]"];

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setToRecipients:recipients];
[recipients release];
[picker setSubject:@"Subject text"];
[picker setMessageBody:@"Message" isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}


I see Rich Love has already done something like this in his "Say it & Mail it" app. Anything to add Rich?
Bernie


To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]