Search code examples
iphoneiosios4xcode4.2

Write .kml files in iOS


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.


Solution

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