将文件保存在iPhone App中的其他位置

时间:2010-05-31 15:29:34

标签: iphone objective-c

我有问题。 我的proget创建一个xml文件。 在iPhone中,此文件存储在NSDocumentDirectory中。 我想将此文件保存在另一个目录中,如桌面(有应用程序)或其他可见文件夹。 感谢。

这是我的代码:

-(void)saveInXML:(NSString*)name:(float)x:(float)y:(float)z{

    //NSDocumentDirectory put the file in the app directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
    NSString *documentsDirectoryPath = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectoryPath  stringByAppendingPathComponent:@"filePosizioni.xml"];

    NSFileHandle *myHandle;
    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *titoloXML = [NSString stringWithFormat:@"File Xml delle posizioni del iPhone"];
    NSString *inizioTag = [NSString stringWithFormat:@"\n\n\n<posizione>"];
    NSString *tagName = [NSString   stringWithFormat:@"\n <name>%@</name>", name];
    NSString *tagX = [NSString  stringWithFormat:@"\n  <x>%f</x>", x];
    NSString *tagY = [NSString  stringWithFormat:@"\n  <y>%f</y>", y];
    NSString *tagZ = [NSString  stringWithFormat:@"\n  <z>%f</z>", z];
    NSString *fineTag= [NSString    stringWithFormat:@"\n</posizione>"];

    NSData* dataTitoloXML = [titoloXML dataUsingEncoding: NSASCIIStringEncoding];
    NSData* dataInizioTag = [inizioTag dataUsingEncoding: NSASCIIStringEncoding];
    NSData* dataName = [tagName dataUsingEncoding: NSASCIIStringEncoding];
    NSData* dataX = [tagX dataUsingEncoding: NSASCIIStringEncoding];
    NSData* dataY = [tagY dataUsingEncoding: NSASCIIStringEncoding];
    NSData* dataZ = [tagZ dataUsingEncoding: NSASCIIStringEncoding];
    NSData* dataFineTag = [fineTag dataUsingEncoding: NSASCIIStringEncoding];

    if(![fileManager fileExistsAtPath:filePath])
        [fileManager createFileAtPath:filePath  contents:dataTitoloXML attributes:nil];

    myHandle  = [NSFileHandle fileHandleForUpdatingAtPath:filePath];

    [myHandle seekToEndOfFile];
    [myHandle   writeData:dataInizioTag];
    NSLog(@"writeok");

    [myHandle seekToEndOfFile];
    [myHandle   writeData:dataName];
    NSLog(@"writeok");

    [myHandle seekToEndOfFile];
    [myHandle   writeData:dataX];
    NSLog(@"writeok");

    [myHandle seekToEndOfFile];
    [myHandle   writeData:dataY];
    NSLog(@"writeok");

    [myHandle seekToEndOfFile];
    [myHandle   writeData:dataZ];
    NSLog(@"writeok");

    [myHandle seekToEndOfFile];
    [myHandle   writeData:dataFineTag];
    NSLog(@"writeok");

    [myHandle seekToEndOfFile];

    NSLog(@"zp26 %@",filePath);


}

1 个答案:

答案 0 :(得分:2)

iPhone上没有类似的可见文件。您有应用程序,他们以他们认为合适的任何方式管理文件和数据。您不能在主屏幕上放置XML文件。