I’m working on an app that writes data out to a .plist from a view and then pulls it in to populate some UILabels in another view. I’ve done this several times in the past and it has worked fine on both the device and simulator. This time it’s working fine on the simulator but not on the device. I’ve been through looking at all the connections and code but just can’t find why it’s not functioning. My suspicion is that for some reason the data is not at the end of the file path, but if it isn’t I can’t understand why this is the case.
I’ve Also tried cleaning the targets through the menu and deleting and reinstalling the app on the testing device in the hope that this may rectify the situation, but no joy.
Can anyone offer some insight into why I’m having this problem and how to rectify it? It’s why it works on the simulator but not the phone that is confusing me, especially when I’ve used the same code in the past to make fully functioning apps. I’m using xCode 4 and testing on an iphone 4:
The code is as follows. First I check to see if there is data at the end of a data file path like this:
- (NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirctory = [paths objectAtIndex:0];
return [documentsDirctory stringByAppendingPathComponent:memberDetails];
}
Then in viewWillAppear I get the data and use it to populate UIlabels in the View. a Few of the labels are visible or invisible based on the data they contain. The viewWillAppear is as follows:
-(void)viewWillAppear:(BOOL)animated
{
// Check data file path and if it exists, load data from there.
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
///All of the following handles the reloading of data from the plist.
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
memName.text = [array objectAtIndex:0];
memAddress.text = [array objectAtIndex:1];
memOccupation.text = [array objectAtIndex:2];
sherex.text = [array objectAtIndex:3];
if ([[array objectAtIndex:3] isEqualToString:@"winstonwinston"]) {
cvName.hidden = NO;
memName.hidden = NO;
cvAddress.hidden = NO;
memAddress.hidden = NO;
cvOccupation.hidden = NO;
memOccupation.hidden = NO;
sherex.hidden = YES;
altText.hidden = YES;
}
else {
cvName.hidden = YES;
memName.hidden = YES;
cvAddress.hidden = YES;
memAddress.hidden = YES;
cvOccupation.hidden = YES;
memOccupation.hidden = YES;
sherex.hidden = YES;
altText.hidden = NO;
}
[array release];
}
[super viewWillAppear:animated];
}
One thing perhaps worth pointing out is that the if/else statement doesn’t seem to be working on the device either. In other words everything in the view is visible. To me this would seem to suggest that the problem might not be the data since if should be able to determine whether or not there is an item called ‘winstonwintson’ in existence or not and display the hidden/showing elements of the view accordingly.
If anyone has some insight please help! I’m totally stuck. Thanks for taking the time to read this.
If it involves files and works on the simulator but not on the device it’s usually one of two problems.
you use filenames that work ok on a case insensitive file system (the simulator) but not on the device (case sensitive filesystem).
The only way I can imagine how this affects you is when you try to copy an initial dataset to the documents directory. For example if you try to copy from your bundle, check the path that is returned by
[[NSBundle mainBundle] pathForResource:@"foo" ofType:@"bar"];If the path is nil you have to check your filenames again, they have to be exactly the same.you write to directories that are not writeable on the device.
One very unlikely possibility is that memberDetails contains something like
/../fooand you try to write in a non writeable directory. On the simulator you can write your files wherever you want, on the device such accesses will fail.So the two questions you should try to answer are:
dataFilePathlook?)EDIT:
NSLog is always a great help in debugging such problems, just print out your paths and check if they are ok.
Something like this:
or you could use a
ifconditionEDIT2:
Since neither of those view configuring parts are called most likely
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])is never true. This means the file does not exist.