Attach an image to an email - ios

I was wondering if there is a way to allow the user to select an image from the camera roll, and then attach it to an email?
Here is the code I have now:
-(IBAction) openEmail {
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
[mailComposer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[mailComposer setToRecipients:[NSArray arrayWithObjects:#"TPsecondary_Example#email.com", nil]];
[mailComposer setSubject:#"Learning Trail Submission"];
[mailComposer setMessageBody:emailbody isHTML:NO];
[mailComposer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
NSString *path = [[NSBundle mainBundle] pathForResource:#"Answer" ofType:#"plist"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[mailComposer addAttachmentData:myData mimeType:#"application/xml" fileName:#"Answer.plist"];
[self presentModalViewController:mailComposer animated:YES];
}
}

There sure is!
In your .h file add these delegates and declare a UIImage named selectedImage.
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
Then in your .m you can add the following.
Link -(IBAction)openImagePicker:(id)sender to the button that you want to start the process.
- (IBAction)openImagePicker:(id)sender
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = NO;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:YES completion:^{
[self openEmail];
}];
}
-(IBAction) openEmail {
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
[mailComposer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[mailComposer setToRecipients:[NSArray arrayWithObjects:#"TPsecondary_Example#email.com", nil]];
[mailComposer setSubject:#"Learning Trail Submission"];
[mailComposer setMessageBody:emailbody isHTML:NO];
[mailComposer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
NSString *path = [[NSBundle mainBundle] pathForResource:#"Answer" ofType:#"plist"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[mailComposer addAttachmentData:myData mimeType:#"application/xml" fileName:#"Answer.plist"];
NSData *imageData = UIImageJPEGRepresentation(selectedImage, 1.0);
[mailComposer addAttachmentData:imageData mimeType:#"image/jpg" fileName:#"imageTitle"];
[self presentModalViewController:mailComposer animated:YES];
}
}
EDIT: Mind you this is a very basic example that doesn't handle events such as the user selecting a video instead of an image...

Related

UIButton's IBAction only works the first time it is tapped

Using Apple's template "Tabbed Bar Banner" I found that the button in one of the views only works the first times I pressed it.
If the iPhone is connected to Xcode to run the app, it works great. Once I have detached the cable, the button stops working.
- (IBAction)sendData:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:YES completion:^{
[self openEmail];
}];
}
-(IBAction) openEmail {
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
[mailComposer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[mailComposer setToRecipients:[NSArray arrayWithObjects:#"*******", nil]];
[mailComposer setSubject:self.nameTextField.text];
NSLog(#"self.nameTextField.text = %#", *********);
[mailComposer setMessageBody:#"******r" isHTML:NO];
[mailComposer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
NSString *path = [[NSBundle mainBundle] pathForResource:#"Answer" ofType:#"plist"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[mailComposer addAttachmentData:myData mimeType:#"application/xml" fileName:#"Answer.plist"];
NSData *imageData = UIImageJPEGRepresentation(selectedImage, 0.5);
[mailComposer addAttachmentData:imageData mimeType:#"image/jpg" fileName:[NSString stringWithFormat:#"%#.jpg",*********]];
[self composeCSV];
NSString *fileName = [file lastPathComponent];
[mailComposer addAttachmentData:[NSData dataWithContentsOfFile:file] mimeType:#"text/csv" fileName:fileName];
[self presentViewController:mailComposer animated:YES completion:nil];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)composeCSV{
NSMutableString *mainString = [[ NSMutableString alloc]initWithString:#"++;++;++;++;...\n"];
[mainString appendFormat:#"%#;\n",...];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
file = [NSString stringWithFormat:#"%#/%#.csv",documentsDirectoryPath,******];
NSError *csVerror= NULL;
BOOL written = [mainString writeToFile:file atomically:YES encoding:NSUTF8StringEncoding error:&csVerror];
if (!written) {
NSLog( #"Writing failed, error = %#",csVerror);
}else {
NSLog(#"Data saved! File path = %#",file);
}
}

UIImagePickerControllerMediaURL always nil?

I'm trying to pick a video from PhotoAlbum. I'm using the UIImagePickerController but every time after i chose the video UIImagePickerControllerMediaURL is returning nil.
What I am missing?
Thanks.
- (IBAction)galerry:(id)sender {
self.imagePickerController = [[UIImagePickerController alloc] init];
[self.imagePickerController setDelegate:self];
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
self.imagePickerController.mediaTypes =#[(NSString *)kUTTypeMovie,(NSString *)kUTTypeVideo];
[self.imagePickerController setVideoMaximumDuration:20];
self.imagePickerController.allowsEditing = YES;
[self presentViewController:self.imagePickerController animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *videoURL= [info objectForKey:UIImagePickerControllerMediaURL];
NSURL *videoUrl=(NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
NSString *moviePath = [videoUrl path];
NSURL *imagePickerURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSURL *videoURL2= [info valueForKey:UIImagePickerControllerMediaMetadata];
// picker.videoQuality=UIImagePickerControllerQualityTypeMedium;
[picker dismissViewControllerAnimated:YES completion:^{
Edition *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"EditionVC"];
vc.videoUrl=videoURL;
[self presentViewController:vc animated:YES completion:nil];
}];
}
All variables returning nil (videoURL,videoUrl,imagePickerURL,videoURL2).
What is wrong here?
EDIT:
This is my info dictionary:
po info
{
UIImagePickerControllerMediaType = "public.movie";
UIImagePickerControllerReferenceURL = "assets-library://asset/asset.MOV?id=87B8D3F5-DE9C-4F06-A88B-5116055A618A&ext=MOV";
}
#import <MobileCoreServices/UTCoreTypes.h>
- (void)getVideo {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = #[ (NSString *) kUTTypeMovie ];
[self presentViewController:imagePicker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = info[ UIImagePickerControllerMediaType ];
if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
NSURL *videoUrl=(NSURL*) info[ UIImagePickerControllerMediaURL ];
NSString *moviePath = [videoUrl path];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
UISaveVideoAtPathToSavedPhotosAlbum (moviePath, nil, nil, nil);
}
}
[self dismissViewControllerAnimated:YES completion:nil];
}

How do I email a pdf using the MFMailComposeViewContoller?

I have a pdf file called FlashCards.pdf
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
NSArray *recipients = #[#"aarone_2010#hotmail.com"];
// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:#"FlashCards" ofType:#"pdf"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"application/pdf" fileName:#"FlashCards"];
[picker setSubject:#"Flashcards"];
[picker setToRecipients:recipients];
[picker setMessageBody:#"Hello" isHTML:NO];
[picker setMailComposeDelegate:self];
[self presentViewController:picker animated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];
The email is being sent and it seems that everything is working, but it's not sending the PDF file.
EDIT:
This actually works, I had other problems in other parts of my code. My problem is solved. I also made sure that the extensions were correct instead of having FlashCards as a file name, it should be FlashCards.pdf This is the exact code I have working:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
NSArray *recipients = #[#"android.aaron.david#gmail.com"];
NSString *path = [[NSBundle mainBundle] pathForResource:#"FlashCards" ofType:#"pdf"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"application/pdf" fileName:#"FlashCards.pdf"];
[picker setSubject:#"Flashcards"];
[picker setToRecipients:recipients];
[picker setMessageBody:#"Hello" isHTML:NO];
[picker setMailComposeDelegate:self];
[self presentViewController:picker animated:YES completion:nil];
Use MIME type text/x-pdf instead of application/pdf. Also check the size/length of myData to verify that the PDF was loaded.

How can I send emails from my custom UIActivity class?

I made a costume UIActivity class. I like to send emails with attachments with the class, but I can't send emails, or present any ViewControllers from the class. I am trying to present the Mail ViewController with this:
- (void)prepareWithActivityItems:(NSArray *)activityItems
{
NSString *subject = [NSString stringWithFormat:#"%#", [self.filePath lastPathComponent]];
NSString *messageBody = [NSString stringWithFormat:#"%# was extracted with #FilyForiOS, visit", [self.filePath lastPathComponent]];
NSData *attachment = [NSData dataWithContentsOfFile:self.filePath];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:subject];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:nil];
[mc addAttachmentData:attachment mimeType:[[self.filePath lastPathComponent] pathExtension] fileName:[self.filePath lastPathComponent]];
[self presentViewController:mc animated:YES completion:nil]; // Here is the error: No visible #interface for 'MailTo' declares the selector 'presentViewController:animated:completion:'
}
Can anyone tell me how I can present a ViewController from this class?
I used this code: How can I create a custom UIActivity in iOS?
Sorry that I'm just now answering this (I know this was asked along time ago) But here is my answer:
#import <MessageUI/MessageUI.h>
#interface EmailActivity : UIActivity <MFMailComposeViewControllerDelegate>
#end
#implementation EmailActivity
- (NSString *)activityTitle
{
// Your title
return #"Email";
}
- (UIImage *)activityImage
{
// Your image
return [UIImage imageNamed:#"Email"];
}
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
return YES;
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self activityDidFinish:YES];
}
- (UIViewController *)activityViewController
{
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc]init];
if ([MFMailComposeViewController canSendMail])
{
NSString *subject = [NSString stringWithFormat:#"%#", [self.filePath lastPathComponent]];
NSString *messageBody = [NSString stringWithFormat:#"%# was extracted with #FilyForiOS, visit", [self.filePath lastPathComponent]];
NSData *attachment = [NSData dataWithContentsOfFile:self.filePath];
[mc setSubject:subject];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:nil];
[mc addAttachmentData:attachment mimeType:[[self.filePath lastPathComponent] pathExtension] fileName:[self.filePath lastPathComponent]];
mc.mailComposeDelegate = self;
return mc;
}
else
{
return nil;
}
}
#end

MFMailComposeViewController display positioning

When I call Apple's MFMailComposeViewController class to send an email from my app, the placement of the To, Cc, Bcc and Subject is a little off. They appear about 1/2 the font size down further than they are supposed to be (when you click the edit the subject, for example, you can only see the top half of the text). This happens even when I copy and paste Apple's sample right into my code. Has anyone seen this before? I've been searching through forums and can't see anybody else who has experienced this.
Code I'm using
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Hello from California!"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:#"first#example.com"];
NSArray *ccRecipients = [NSArray arrayWithObjects:#"second#example.com", #"third#example.com", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:#"fourth#example.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Fill out the email body text
NSString *emailBody = #"It is raining in sunny California!";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
Try the below code:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSString * subj = [NSString stringWithFormat:#"" ];
[picker setSubject:subj];
// Set up recipients
NSArray *toRecipients = [[NSArray alloc] initWithObjects:#"first#example.com",nil];
NSArray *ccRecipients = [[NSArray alloc] initWithObjects::#"second#example.com",#"third#example.com", nil];
NSArray *bccRecipients = [[NSArray alloc] initWithObjects::#"fourth#example.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients]
[toRecipients release];
[ccRecipients release];
[bccRecipients release];
NSString *body = #"";
[picker setMessageBody:body isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];

Resources