Email app from another app
I want to start an email app from app on button pressed. But when I pressed 开发者_Python百科the button nothing is happening !!!
code :
- (IBAction) startMail
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:emailAdress?subject=testMail&body=its test mail."]];
}
any thing wrong in code ? Also button is properly set in IB.
Thanks..
If you want to compose an EMail inside your app, you should have a look at the MFMailComposeViewController reference to do so instead of calling a mailto: URL scheme.
Using the mailto URL won't work in the simulator as mail.app isn't installed on the simulator. It does work on device though.
You can use "MFMailComposeViewController" to send mail from your application.
Sample code-
MFMailComposeViewController *picker;
picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSString *objSubject = [[NSString alloc] init];<br/>
NSString *emailBody = [[NSString alloc] init];
[picker setSubject:objSubject];
[picker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:picker animated:YES];
You must use the following delegate to check the status of mail sent or not
**-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
}**
Enjoy
Try this:
NSString *mailString = [NSString stringWithFormat:@"mailto:?to=%@&subject=%@&body=%@",
[to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailString]];
Here to, subject and body are NSString variables
Check this out, it's a better solution than directing your user out of the app;
How can I send mail from an iPhone application
NSString *url = [NSString stringWithString: @"mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
精彩评论