How do I email a pdf using the MFMailComposeViewContoller? - ios

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.

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);
}
}

Unable to attatch a csv file to mail in objective c

I'm using the following code to attach a csv file that is generated to mail composer.
NSString *recipient = #"tejanvm92#gmail.com";
NSArray *recipients = [NSArray arrayWithObjects:recipient, nil];
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:#"CSV Export"];
[mailViewController setToRecipients:recipients];
[mailViewController setMessageBody:#"" isHTML:NO];
mailViewController.navigationBar.tintColor = [UIColor blackColor];
NSData *myData = [NSData dataWithContentsOfFile:#"yourFileName.csv"];
[mailViewController addAttachmentData:myData
mimeType:#"text/csv"
fileName:#"yourFileName"];
[self presentModalViewController:mailViewController animated:YES];
When the code runs.....the csv file is not attached to the mail properly.
But When I got the mail, it is empty. Could you please help me in finding the issue?
[NSData dataWithContentsOfFile:#"yourFileName.csv"]// its wrong way.
Please write full path of csv file
Like if your csv file in Documents directory.
then
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *csvFilePath = [documentsDirectory stringByAppendingFormat:#"/yourFileName.csv"];
after that change in your code
[NSData dataWithContentsOfFile:csvFilePath];

Attach an image to an email

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...

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];

How to add gif to attachment in iPhone mail?

I add .jpg this way:
UIImage *sentPic = self.image;
NSData *imageData = UIImageJPEGRepresentation(sentPic, 1);
[picker addAttachmentData:imageData mimeType:#"image/jpg" fileName:#"pic.jpg"];
I change it to "image/gif".But it doesn't work fine. Th Gif is just a still image in the mail.
NSData *imageData = [[NSData alloc] initWithContentsOfFile:pathToGifFile];
[picker addAttachmentData:imageData mimeType:#"image/gif" fileName:#"pic.gif"];
[imageData release];
This would add it as attachment, but I'm not sure if that's what you want, or you want to display the image inside the message?
Have you ever tried HTML code in your email like this:
NSString *emailBody = #"It is a gif email!";
emailBody = [EmailBody stringByAppendingString:#"<img src='http://xx.xx.pic.gif'>"];
[picker setMessageBody:emailBody isHTML:YES];
The code I haven't tested, you can have a try. Tell us if it works.
I just tested this. Works great:
NSString *pathForGif = [[NSBundle mainBundle] pathForResource: #"<your file name>" ofType: #"gif"];
NSData *gifData = [NSData dataWithContentsOfFile: pathForGif];
[mailController addAttachmentData:gifData mimeType:#"image/gif" fileName:#"EmailGIF.gif"];
[self presentModalViewController:mailController animated:YES];

Resources