In my app I need to store the data in a plist file to use in another UIViewController
and to keep a tiny db on the app. I made so:
- I get the information that I need to store in this file
- I inserted this information in an
NSDictionary
- I inserted the
NSDictionary
in an NSMutableArray
- Now I made an utility class that handle the work with file
But it doesn't create the plist file in the Documents directory. Here's my code:
ViewController.m
- (void)saveActivity {
if ([FileHander plistExist]) {
NSMutableArray *dataFromFile = [[NSMutableArray alloc]init];
dataFromFile = [FileHander loadDataFromFile];
if (![[dataFromFile lastObject]objectForKey:@"type"]) {
[FileHander saveFileWithArray:arrayActivity];
} else {
[dataFromFile addObject:arrayActivity];
[FileHander saveFileWithArray:dataFromFile];
}
} else {
NSLog(@"%@", arrayActivity);
[FileHander saveFileWithArray:arrayActivity];
}
}
the arrayActivity
contains this (coordinate, speed, time etc...):
(
{
coordinates = (
"<+45.44083526,+9.19919774> +/- 10.00m (speed 0.00 mps / course 71.02) @ 04/02/14 12:46:08 Ora standard dell'Europa centrale",
"<+45.44083561,+9.19919755> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
"<+45.44083612,+9.19919728> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
"<+45.44083663,+9.19919700> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
"<+45.44083714,+9.19919673> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
"<+45.44084181,+9.19917132> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
"<+45.44083100,+9.19919440> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
"<+45.44088744,+9.19913454> +/- 50.00m (speed 0.00 mps / course -1.00) @ 04/02/14 12:46:35 Ora standard dell'Europa centrale",
"<+45.44088744,+9.19913454> +/- 30.00m (speed 0.00 mps / course -1.00) @ 04/02/14 12:46:36 Ora standard dell'Europa centrale",
"<+45.44088744,+9.19913454> +/- 30.00m (speed 0.00 mps / course -1.00) @ 04/02/14 12:46:37 Ora standard dell'Europa centrale",
"<+45.44088744,+9.19913454> +/- 30.00m (speed 0.00 mps / course -1.00) @ 04/02/14 12:46:39 Ora standard dell'Europa centrale"
);
speed = (
);
steps = 16;
time = "00:00:08";
type = Trekking;
}
)
the utility class is this:
FileHandler.m
#import "FileHander.h"
@implementation FileHander
+ (BOOL) plistExist {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *path = [documentsDir stringByAppendingPathComponent:@"activity.plist"];
return [[NSFileManager defaultManager]fileExistsAtPath:path];
}
+ (NSMutableArray *) loadDataFromFile {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *path = [documentsDir stringByAppendingPathComponent:@"activity.plist"];
NSMutableArray *dataFound = [NSArray arrayWithContentsOfFile:path];
return dataFound;
}
+ (BOOL) deletePlist {
NSFileManager *manager = [NSFileManager defaultManager];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"activity.plist"];
NSError *error;
BOOL success =[manager removeItemAtPath:filePath error:&error];
if (success) {
return YES;
}
else
{
return NO;
}
}
+ (void) saveFileWithArray: (NSMutableArray*)activityArray {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"activity.plist"];
[activityArray writeToFile:fileName atomically:YES];
}
@end
When I execute this app it can't create the plist file on the device (I looked for this file by using the Mac App iExplorer), what's wrong in my code?
I used the same code to develop another app and it works great.
I hope you can help me to fix the issue.
Thank you