Reputation: 113
I'm parsing data from a file if the internet connection is unavailable. When I test this in the iphone simulator it works perfectly. when I switch to the device it fails. When I save and load the data I used the same name with no difference in case. What could be the problem?
- (void)parseXMLFileAtURL:(NSString *)URL
{
events = [[NSMutableArray alloc] init];
NSURL *xmlURL = [NSURL URLWithString:URL];
NSString* xml = [[NSString alloc]initWithContentsOfURL:xmlURL];
NSData * xmlData = [xml dataUsingEncoding:NSUTF8StringEncoding]; //[[NSData alloc]initWithContentsOfURL:xmlURL];
if (xmlData != nil) {
fileParser = [[NSXMLParser alloc] initWithData:xmlData];
HBSaveData(xmlData, @"/xml.plist");
NSLog(@"backup made %@ " ,xmlData);
} else {
xmlData = [[[NSData alloc]initWithData:HBLoadData(@"/xml.plist")]retain];
fileParser = [[NSXMLParser alloc]initWithData:xmlData];
networkConnected = YES;
NSLog(@"backup used %@ " ,xmlData);
}
[fileParser setDelegate:self];
[fileParser setShouldProcessNamespaces:NO];
[fileParser setShouldReportNamespacePrefixes:NO];
[fileParser setShouldResolveExternalEntities:NO];
[fileParser parse];
}
This is the second issue i've had like this and I don't understand why. I've used this same method of saving files 3 other times and now this time it doesn't want to work. My other question remains unanswered.
Here are the save and load functions
static void HBSaveData(NSData *data, NSString* savePath){
NSString *filePath = pathOfFile(savePath);
NSString *errString;
NSData *serialized =
[NSPropertyListSerialization dataFromPropertyList:data
format:NSPropertyListBinaryFormat_v1_0
errorDescription:&errString];
[serialized writeToFile:savePath atomically:YES];
if (errString)
{
NSLog(@"%@" ,errString);
[errString release]; // exception to the rules
}
//[data writeToFile:filePath atomically:YES];
NSLog(@"%@", filePath);
}
static NSData* HBLoadData(NSString* savePath) {
NSString *filePath = pathOfFile(savePath);
NSString *errString;
NSData *serialized = [NSData dataWithContentsOfFile:savePath];
NSData* data =
[NSPropertyListSerialization propertyListFromData:serialized
mutabilityOption:NSPropertyListMutableContainers
format:NULL
errorDescription:&errString];
if (errString)
{
NSLog(@"%@" ,errString);
[errString release]; // exception to the rules
}
return data;
}
static inline NSString* pathOfFile(NSString * savePath){
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsFolder = [paths objectAtIndex:0];
return [documentsFolder stringByAppendingPathComponent:savePath];
}
I performed a test and looked at the contents of the file directory at read and write. on the simulator I have this.
2011-03-31 08:05:39.117 AnimeSTL[5992:207] All documents ( "myEventId.plist", "myEvents.plist", "myListEvents.plist", "xml.dat" )
On the device I get this
2011-03-31 08:11:10.019 AnimeSTL[134:307] All documents ( "myEventId.plist", "myEvents.plist", "myListEvents.plist" ) the file isn't being saved for some reason.
edit: The write fails so I output the file path anyway. is it because iphones can't handle files of type .dat?
2011-03-31 09:48:50.954 AnimeSTL[196:307] All documents ( "myEventId.plist", "myEvents.plist", "myListEvents.plist" ) 2011-03-31 09:48:50.960 AnimeSTL[196:307] write failed 2011-03-31 09:48:50.962 AnimeSTL[196:307] /var/mobile/Applications/FCB4AA53-5450-495B-A190-930535335301/Documents/xml.dat
Upvotes: 0
Views: 547
Reputation: 2222
Is /xml.plist
the full path of your file (xml.plist in the root folder), or is that path being modified in HBSaveData()
?
If that is the actual path, that's probably your problem – iOS only allows you to write to the filesystem in folders specific to your app. Look at using NSSearchPathForDirectoriesInDomains()
to find your app's documents folder.
Upvotes: 0
Reputation: 125037
Remember that the iOS file system is case sensitive, whereas the Mac OS X file system usually isn't. So if your file is really called "XML.plist" instead of "xml.plist", that'll work on the simulator but not on the device.
Upvotes: 1