Write .kml files in iOS - iphone

I would like to know how can i write to a file within Application. The thing im trying to do here is, i am generating a KML (using class XMLWriter from https://github.com/skjolber/xswi). I can log the KML, everythings fine, now what i have to do is, Same this KML to a .kml file, and then email it to some address.
Emailing is no problem, i wanted to know how to write this KML into a .kml file, and then retrieve it for emailing. Can someone help me with a code snippet.

This tutorial did it, more then help.. The Code for generating the .kml file:
NSFileManager *filemgr;
NSData *databuffer;
NSString *dataFile;
NSString *docsDir;
NSArray *dirPaths;
filemgr = [NSFileManager defaultManager];
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
dataFile = [docsDir stringByAppendingPathComponent: #"datafile.kml"];
databuffer = [[xmlWriter toString] dataUsingEncoding: NSASCIIStringEncoding];
[filemgr createFileAtPath: dataFile contents: databuffer attributes:nil];
then to retrieve it i used:
NSFileManager *filemgr;
NSString *dataFile;
NSData *databuffer;
NSString *docsDir;
NSArray *dirPaths;
filemgr = [NSFileManager defaultManager];
// Identify the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the data file
dataFile = [docsDir stringByAppendingPathComponent: #"datafile.kml"];
databuffer = [filemgr contentsAtPath: dataFile];

I believe you've got all your content into a string, if so you can proceed with saving it to .kml file with something like this:
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fname = [tmp_FileName stringByAppendingFormat: #".kml" ];
NSString* filenameStr = [documentsDirectory
stringByAppendingPathComponent:fname];
[yourKMLString writeToFile: filenameStr atomically: YES];

Related

How to rename files from Documents directory?

I am saving my files in Document directory named 1.png, 2.png, 3.png, 4.png, 5.png. If I delete the original file 3.png, how do I rename files 4.png and 5.png to be called 3.png and 4.png respectively?
This is the code I am using to write the files in the first place:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[appDel.photo addObject:#"photo"];
NSString *imageName = [NSString stringWithFormat:#"%i.png", [appDel.photo count]];
appDel.numberPhoto = [appDel.photo count];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
imageName];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
To get NSDocuments directory use :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"MyFile.txt"];
How to rename file you can find here:
How to rename a file using NSFileManager
If you do not know the names of the files in documents directory you can use :
NSArray *directoryContent = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];
This array contains all filenames you need. So you can rename them all.

Generate CSV sample iphone app/code

Does anyone have a sample app or some sample code to generate a CSV file on iOS?
Here is a simple code to generate CSV file :
NSArray *csvArray = [myTextView.text componentsSeparatedByString:#"\n"];
NSString *csvString = [csvArray componentsJoinedByString:#","];
NSLog(#"csvString:%#",csvString);
// Create .csv file and save in Documents Directory.
//create instance of NSFileManager
NSFileManager *fileManager = [NSFileManager defaultManager];
//create an array and store result of our search for the documents directory in it
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//create NSString object, that holds our exact path to the documents directory
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(#"Document Dir: %#",documentsDirectory);
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.csv", #"userdata"]]; //add our file to the path
[fileManager createFileAtPath:fullPath contents:[csvString dataUsingEncoding:NSUTF8StringEncoding] attributes:nil]; //finally save the path (file)
Following code I used for generating CSV.
NSMutableString *mainString=[[NSMutableString alloc]initWithString:#""];
for(int i=0;i<[commentArray count];i++ ) {
NSString *string=[[commentArray objectAtIndex:i]objectForKey:#"cmtName"];
string=[string stringByReplacingOccurrencesOfString:#"\"" withString:#"\"\""];
[mainString appendFormat:#"\"%#\"",string];
string=[[commentArray objectAtIndex:i]objectForKey:#"cmtDesc"];
string=[string stringByReplacingOccurrencesOfString:#"\"" withString:#"\"\""];
[mainString appendFormat:#",\"%#\"",string];
string=[[commentArray objectAtIndex:i]objectForKey:#"cmtType"];
string=[string stringByReplacingOccurrencesOfString:#"\"" withString:#"\"\""];
[mainString appendFormat:#",\"%#\"",string];
[mainString appendFormat:#",\"%#\"",string];
[mainString appendFormat:#"\n"];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:#"Docment.conf"];
NSData* settingsData;
settingsData = [mainString dataUsingEncoding: NSASCIIStringEncoding];
if ([settingsData writeToFile:filePath atomically:YES])
NSLog(#"writeok");
And for importing CSV you use this
https://github.com/davedelong/CHCSVParser
My CHCSVParser will both read and write CSV files on OS X and iOS. It comes with some examples of how to use it.

How to load PDFs from Documents Directory?

I can load list of pdf files(all pdf files) from Resources folder, using this code.
NSArray *pdfs = [[NSBundle mainBundle] pathsForResourcesOfType:#"pdf" inDirectory:nil];
But I couldn't find a method to load list of pdf files from Documents Directory. The following line of code can only load one pdf at a time.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingPathComponent:#"test.pdf"];
NSURL *urlPdf = [NSURL fileURLWithPath: file];
How could I load all pdf files from Documents Directory
Can you please give me a code example.
I was able to solve my issue using this line of code :)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *pdfs = [[NSBundle bundleWithPath:[paths objectAtIndex:0]] pathsForResourcesOfType:#"pdf" inDirectory:nil];
Go with NSFileManager's contentsOfDirectoryAtPath and filter the result by hand for pdf files.
This answer maybe helpful.
You could do this
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
NSArray *filesAtPath = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:error];
for (NSString *path in filesAtPath)
{
if ([path rangeOfString:#".pdf"].length > 0)
{
//Do whatever you want with the pdf file
}
}
I hope it helps, cheers

Problem writing NSData in my device

Can someone explain me why my code doesn't work?
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the
//documents directory:
NSString *fullFileName = [NSString stringWithFormat:#"%#/subscriptions", documentsDirectory];
[dataReply writeToFile:fullFileName atomically:NO];
NSData *getData=[[NSData alloc]initWithContentsOfFile:fullFileName];
infact if I try
NSString *string = [[NSString alloc] initWithData:getData encoding:NSASCIIStringEncoding];
while if I load myData without save it to my device it works! (So I'm sure that my data is not empty)
Try This,
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the
//documents directory:
NSString *fullFileName = [NSString stringWithFormat:#"%#/subscriptions", documentsDirectory];
[dataReply writeToFile:fullFileName atomically:NO];
Nsdata *getData=[fullFileName dataUsingEncoding:NSUTF8StringEncoding];

saving data in iphone

I am trying to download an image and audio file from the url.. i want to save the downloaded files in specific path is it possible..
please suggest me...
thanks...
save the files in the documents directory of your app.
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [documentsDirectoryPath stringByAppendingPathComponent:#"myfile.mp3"];
BOOL success = [myMp3Data writeToFile:path atomically:YES];
Yes, it is possible.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath2 = [NSString stringWithFormat:#"%#/%#.jpg", documentsDirectory, fileName];
NSData* imageData = UIImageJPEGRepresentation(drawImage.image, 1.0);
[imageData writeToFile:filePath2 atomically:NO];