Comment envoyer un e-mail dans une application sans quitter l'application.
Cela marche:
-(void) sendEmailTo:(NSString *)to withSubject:(NSString *)subject withBody:(NSString *)body {
NSString *mailString = [NSString stringWithFormat:@"mailto:?to=%@&subject=%@&body=%@",
[to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailString]];
}
mais va à l'application de messagerie pour envoyer. Existe-t-il un moyen de le faire sans quitter l'application?
Oui. Utilisez le MFMailComposeViewController .
// From within your active view controller
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
mailCont.mailComposeDelegate = self;
[mailCont setSubject:@"yo!"];
[mailCont setToRecipients:[NSArray arrayWithObject:@"[email protected]"]];
[mailCont setMessageBody:@"Don't ever want to give you up" isHTML:NO];
[self presentViewController:mailCont animated:YES completion:nil];
}
// Then implement the delegate method
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self dismissViewControllerAnimated:YES completion:nil];
}
Ajouter un framework MessageUI:
Dans la vue actuelle, le contrôleur ajoute l'importation et implémente un protocole:
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
@interface MyViewController : UIViewController<MFMailComposeViewControllerDelegate>
Ajouter des méthodes:
-(void)sendEmail {
// From within your active view controller
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
mailCont.mailComposeDelegate = self; // Required to invoke mailComposeController when send
[mailCont setSubject:@"Email subject"];
[mailCont setToRecipients:[NSArray arrayWithObject:@"[email protected]"]];
[mailCont setMessageBody:@"Email message" isHTML:NO];
[self presentViewController:mailCont animated:YES completion:nil];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[controller dismissViewControllerAnimated:YES completion:nil];
}
Mis à jour pour iOS 6. Veuillez noter que cela utilise ARC et n'utilise pas la présentation de la vue modale obsolète:
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
@interface MyViewController : UIViewController<MFMailComposeViewControllerDelegate>
Et puis le code pour présenter l'écran email:
- (IBAction)emailButtonPushed:(id)sender {
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
mailCont.mailComposeDelegate = self;
[mailCont setSubject:@"Your email"];
[mailCont setMessageBody:[@"Your body for this message is " stringByAppendingString:@" this is awesome"] isHTML:NO];
[self presentViewController:mailCont animated:YES completion:nil];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
//handle any error
[controller dismissViewControllerAnimated:YES completion:nil];
}