Envoyer un file en pièce jointe dans l'objective c

Je souhaite envoyer un file (image) en pièce jointe en le sélectionnant dans un sélecteur d'image. Quelle est la manière appropriée de joindre et d'envoyer un file (en particulier une image) dans iOS Objective-C?

Utilisez la méthode ci-dessous

-(void)displayComposerSheet { MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; [picker setSubject:@"Check out this image!"]; // Set up recipients // NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"]; // NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]; // NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"]; // [picker setToRecipients:toRecipients]; // [picker setCcRecipients:ccRecipients]; // [picker setBccRecipients:bccRecipients]; // Attach an image to the email UIImage *coolImage = ...; NSData *myData = UIImagePNGRepresentation(coolImage); [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"coolImage.png"]; // Fill out the email body text NSSsortingng *emailBody = @"My cool image is attached"; [picker setMessageBody:emailBody isHTML:NO]; [self presentModalViewController:picker animated:YES]; [picker release]; } 

Et implémenter la méthode de délégué

 - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { // Notifies users about errors associated with the interface switch (result) { case MFMailComposeResultCancelled: NSLog(@"Result: canceled"); break; case MFMailComposeResultSaved: NSLog(@"Result: saved"); break; case MFMailComposeResultSent: NSLog(@"Result: sent"); break; case MFMailComposeResultFailed: NSLog(@"Result: failed"); break; default: NSLog(@"Result: not sent"); break; } [self dismissModalViewControllerAnimated:YES]; } 

Et dans votre file d'interface

 #import <MessageUI/MFMailComposeViewController.h> ... @interface ... : ... <MFMailComposeViewControllerDelegate>