Save file in a different location in iPhone App
I have a problem. My proget create a xml file. In the iPhone this file was store in the NSDocumentDirectory. I wanna save this file in another directory like Desktop(where there are the apps) or another visible folder. Thanks.
This is my code:
-(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:filePat开发者_如何学Pythonh];
[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);
}
You don't have visible files like that on the iPhone. You have Apps, and they manage files and data in any way they see fit. You can't put an XML file on the home screen.
精彩评论