How to attach a plist into an email in Xcode? - iphone

I am working on a quiz app where the user's input will be stored inside a plist files. I tried researching for various ways to attach files to an email, but mostly are all for JPEG and TXT files.
I have created a plist and tried to attach the it the same way, but on the mailcomposer it only shows the filename of the attachment.
-(NSString *) dataFilePath
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:#"Answer.plist"];
}
This is the code in the composer
-(IBAction) openEmail {
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
[mailComposer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[mailComposer setToRecipients:[NSArray arrayWithObjects:#"secondary_Example#email.com", nil]];
[mailComposer setSubject:#"Subject Topic"];
[mailComposer setMessageBody:#"Message Body" 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:#"Answers"];
[self presentModalViewController:mailComposer animated:YES]; }}
Am I missing something out? Please help.. Would really appreciate it.

Related

Save PDF then send in email

I am trying to save a pdf from a url and then send it in an email. The sender seems to have it but the receiver does not get it.
When I do a NSLog of the NSString file I get /var/mobile/Applications/0ADE222E-6346-4C6C-8348-DA5327B980AA/Documents/myPDF.pdf
It seems like it saves but it doesn't send. Here is my code below for saving and sending
EDIT
Updated code
// This pdfURL is 0 bytes
NSData *pdfURL = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",webPage.urlString]]];
//Store the Data locally as PDF File
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *file = [documentDirectory stringByAppendingFormat:#"/myPDF.pdf"];
[pdfURL writeToFile:file atomically:YES];
//Sending the pdf
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
composer.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]){
//Changed email for privacy issues
[composer setToRecipients:[NSArray arrayWithObjects:#"123#abc.com", nil]];
[composer setSubject:[NSString stringWithFormat:#"%# email",titleText]];
[composer setMessageBody:#"your custom body content" isHTML:NO];
NSLog(#"pdf %#",file);
// This pdfData is 0 bytes
NSData *pdfData = [NSData dataWithContentsOfFile:file];
[composer addAttachmentData:pdfData mimeType:#"application/pdf" fileName:#"myPDF.pdf"];
[composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:composer animated:YES];
}
If it's from a URL, store it in NSData:
NSData *pdfData = [NSData dataWithContentsOfURL[NSURL URLWithString:#"URL Of Pdf"]];
And then attach that to mail
The issue was with the webPage.urlString being empty. This code works just make sure the url string is not empty :P

how to create and attach CSV file in MFMailComposer in iPhone sdk?

i have created one csv file and i am also attaching it to MFMailComposer and it shows me to my mail composer but when i send it to user email it does not shows me attached csv file in email. i have used this code to create csv file and adding data in it.
NSMutableString *mainString=[[NSMutableString alloc]initWithString:#""];
//NSMutableArray *section = [[NSMutableArray alloc] init];
for(int i = 0;i<[NameArray count];i++)
{
NSString *string=[indexArray objectAtIndex:i];
string=[string stringByReplacingOccurrencesOfString:#"\"" withString:#"\"\""];
[mainString appendFormat:#"\"%#\"",string];
string=[NameArray objectAtIndex:i];
string=[string stringByReplacingOccurrencesOfString:#"\"" withString:#"\"\""];
[mainString appendFormat:#",\"%#\"",string];
string=[typearray objectAtIndex:i];
string=[string stringByReplacingOccurrencesOfString:#"\"" withString:#"\"\""];
[mainString appendFormat:#",\"%#\"",string];
[mainString appendFormat:#",\"%#\"",string];
[mainString appendFormat:#"\n"];
}
NSLog(#"getdatafor csv:%#",mainString);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:#"history.csv"];
// filePath = [filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData* settingsData;
settingsData = [mainString dataUsingEncoding: NSASCIIStringEncoding];
NSError *error;
[settingsData writeToFile:filePath atomically:YES];
// NSLog(#"writeok");
NSData *mediaData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMapped error:&error];
NSLog(#"Length:%d Error:%#",[mediaData length],[error localizedDescription]);
here the above code is working good i am getting [mediaData length] i am attaching the CSV file from here.
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:#"history" ofType:#"csv"];
NSData *myData = [NSData dataWithContentsOfFile:path];
// Fill out the email body text
NSString *emailBody = #"history";
[picker setMessageBody:emailBody isHTML:NO];
[picker addAttachmentData:myData mimeType:#"text/cvs" fileName:#"history"];
[self presentModalViewController:picker animated:YES];
[picker release];
the above code is also working properly. it shows me attached CSV file but when i am sending the mail by email at that time receiver is not getting the attached CSV file. what's wrong in this code.? why the receiver is not getting the attached file.?
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"CSV File"];
NSData *myData = [text dataUsingEncoding:NSUTF8StringEncoding];
[mailer addAttachmentData:myData mimeType:#"text/cvs" fileName:#"FileName"];
[self presentModalViewController:mailer animated:YES];
Where the 'text' is a string.
i have solved this issue with attaching files and other media property in MFMailComposeViewController.
NSData *data=[[arr componentsJoinedByString:#","] writeToFile:#"Bhavesh.csv" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
[mail addAttachmentData:data mimeType:#"text/csv" fileName:#"Bhavesh.csv"];

Can't find vcard attachment with the mail

I am trying to send an email from my iphone, with a vcard as attachment. When I am sending the mail, the vcard is being attached with the mail. But the receiver of the mail can't find the vcard attachment. Help needed.
This is the code I have used
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSString *path = [[NSBundle mainBundle] pathForResource:#"Vcard" ofType:#"vcf"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"text/x-vcard" fileName:#"Vcard.vcf"];
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
Thanks
I found the solution... Isubmitted a bug on Apple radar about it. MFMailcomposer has a bug in which you have to send an image along with your extra attachments in order to get the weird items like a pdf to work... try this and replace the pdf with your card:
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
NSString *emailSubject = [NSString localizedStringWithFormat:#"MedicalProfile"];
[controller setSubject:emailSubject];
NSString *fileName = [NSString stringWithFormat:#"%#.pdf", profileName];
NSString *saveDirectory = NSTemporaryDirectory();
NSString *saveFileName = fileName;
NSString *documentPath = [saveDirectory stringByAppendingPathComponent:saveFileName];
*** YOU MUST INCLUDE AN IMAGE OR THE PDF ATTATCHMENT WILL FAIL!!!***
// Attach a PDF file to the email
NSData *pdfData = [NSData dataWithContentsOfFile:documentPath];
[controller addAttachmentData:pdfData mimeType:#"application/pdf" fileName:fileName];
// Attach an image to the email
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"miniDoc" ofType:#"png"];
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
[controller addAttachmentData:imageData mimeType:#"image/png" fileName:#"doctor"];
[controller setMessageBody:[NSString stringWithFormat:#"%#'s Medical Profile attatched!", profileName] isHTML:NO];
[self presentModalViewController:controller animated:YES];
controller.mailComposeDelegate = self;
[controller release];

How can I convert my Zip-file to NSData to email my Zip file as an attachment

I', m using the Objective Zip library to compress several images i took. I came to the point (I guess) where I' zipping an image.
Now I'd like to send this zipped File with the mailcomposer. However I need to declare a "NSData object" within my mail function.
[picker addAttachmentData:"NSData object" mimeType:#"application/zip" fileName:#"test.zip"];
Here's a snippit of my code
-(IBAction)sendMail{
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"test.zip"];
NSArray *data = [[NSArray alloc] initWithObjects:#"first",#"second",#"third",nil];
NSString *docDir3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath4 = [NSString stringWithFormat:#"%#/foto2.jpg",paths];
[data writeToFile:pngFilePath4 atomically:YES];
NSData * fotoData = [[NSData alloc] initWithContentsOfFile:pngFilePath4];
NSFileManager *manager = [[NSFileManager alloc] init];
[manager removeItemAtPath:pngFilePath4 error:nil];
ZipFile *readFile = [[ZipFile alloc] initWithFileName:path mode:ZipFileModeCreate];
ZipWriteStream *stream = [readFile writeFileInZipWithName:#"foto2.jpg" compressionLevel:ZipCompressionLevelNone];
[stream writeData:fotoData];
[stream finishedWriting];
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate=self;
[picker addAttachmentData:"NSData Object" mimeType:#"application/zip" fileName:#"test.zip"];
Class mailclass = (NSClassFromString(#"MFMailComposeViewController"));
if([mailclass canSendMail]){
[self presentModalViewController:picker animated:YES];
}
[readFile close];
[data2 release];
[fotoData release];
}
I think i need to make another NSData object from the readFile object and place this within the [picker attachmentData: method]. Hope someone can point me in the right direction.
EDIT
Still can't get this to work properly. It takes realy long to send the zip file (Even through wifi). When I open the zip image I get an error which says that the file cannot be opened. Here's my code:
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"test.zip"];
NSArray *data = [[NSArray alloc] initWithObjects:#"first",#"second", nil];
NSString *docDir3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath3 = [NSString stringWithFormat:#"%#/foto2.jpeg",docDir3];
NSData * imageData2 = [[[NSData alloc] initWithContentsOfFile:pngFilePath3] autorelease];
[data writeToFile:pngFilePath3 atomically:YES];
ZipFile *readFile = [[ZipFile alloc] initWithFileName:path mode:ZipFileModeCreate];
ZipWriteStream *stream = [readFile writeFileInZipWithName:#"foto2.jpeg" compressionLevel:ZipCompressionLevelNone];
[stream writeData:imageData2];
[stream finishedWriting];
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate=self;
[picker addAttachmentData:[NSData dataWithContentsOfFile:path] mimeType:#"application/zip" fileName:#"test.zip"];
Class mailclass = (NSClassFromString(#"MFMailComposeViewController"));
if([mailclass canSendMail]){
[self presentModalViewController:picker animated:YES];
}
[data release];
[readFile close];
Thanks in advance for helping me out!
The ZipWriteStream is writing to path, and getting data from a file path is done using [NSData dataWithContentsOfFile:path].
[picker addAttachmentData:[NSData dataWithContentsOfFile:path]
mimeType:#"application/zip"
fileName:#"test.zip"];
Objective-Zip does throw exceptions when a write failure occurs so make sure you add a try/catch around the write operation and make sure your data is not nil.

Using ZipArchive Zip file in iphone mail attachment?

is it possible to send a zip Archive file inside an email attachment using the mail api?
Try This .. This one worked for me
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *WritableDBPath= [documentsDirectory stringByAppendingPathComponent:kFilename];
NSData *data = [NSData dataWithContentsOfFile:WritableDBPath];
[picker addAttachmentData:data mimeType:#"application/zip" fileName:#"/abc.zip"];
[picker setSubject:#"Database"];
[picker setMessageBody:#"Database testing" isHTML:NO];
[self presentModalViewController:picker animated:YES];
Yes it is possible.
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *WritableDBPath= [documentsDirectory stringByAppendingPathComponent:kFilename];
NSData *data = [NSData dataWithContentsOfMappedFile:WritableDBPath];
[picker addAttachmentData:data mimeType:#"text/richtext" fileName:#"/abc.zip"];
[picker setSubject:#"Database"];
[picker setMessageBody:#"Database testing" isHTML:YES];
[self presentModalViewController:picker animated:YES];
You can choose the filepath, filename as per your choice. Please verify the mime-type if its not work.
:)