I came cross this code as shown below.In the following code, I could able to save only one single annotation, however I have an array of annotations, I could not able to save them with single NSUserDefaults
To save:
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setDouble:location.latitude forKey:@"savedCoordinate-latitude"];
[ud setDouble:location.longitude forKey:@"savedCoordinate-longitude"];
[ud setBool:YES forKey:@"savedCoordinate-exists"];
[ud synchronize];
Edited:
-(void)viewDidLoad
NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
if([ud boolForKey:@"save-exist"])
{ NSMutableArray *udAnnotations=[[NSMutableArray alloc]initWithArray:
[ud objectForKey:@"annotationsArray"]];
NSLog(@"%d",[udAnnotations count]);
}
else{
[self addAnno];
}
-(void)addAnno
{
[mapView addAnnotations:annotationArray];
NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
[ud setObject:annotationArray forKey:@"annotationsArray"];
[ud setBool:YES forKey:@"save-exist"];
[ud synchronize];
}
As you already have an array, simply use
setObject:forKey:andarrayForKey:As the objects in your original array are not a type that can be saved directly, store their data in dictionaries:
and convert on retrieval:
A couple notes:
dictionaryWithObjectsAndKeys:andobjectForKey: