Reputation: 21
I have an app that I want a user to select a location from the first time they open up the app. After they select their location, I want the location to be saved and certain buttons to be made available for certain locations. Currently, I have tried saving the User's Location, which I can achieve only when in the app, however, whenever the app is closed out the old data does not load. My two questions are:
-- How do I save a User's Location throughout the Whole App
-- Which way is recommended to call the different links based on the User's Location?
(UPDATE): What I have tried right now is Using NSUSERDEFAULTS. Here's my Code.
- (void)viewDidLoad {
[super viewDidLoad];
[self userLocationSave];
}
-(void)userLocationSave
{
campusLocation = self.lblUserLocation.text;
self.lblUserLocation.textColor = [UIColor whiteColor];
[self saveData];
}
-(void)saveData
{
NSString *saveLocation = self.lblUserLocation.text;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:saveLocation forKey:@"campusChoice"];
[defaults synchronize];
}
-(void)loadData
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadString = [defaults objectForKey:@"campusChoice"];
[self.lblUserLocation setText:loadString];
}
I then load the data in the viewDidAppear Method.
To clarify, campus location is an NSString that comes from a view controller where the user selects the persons data.
Upvotes: 0
Views: 68
Reputation: 9687
It seems like you are not actually saving the users app if the data is not there during the next session.
There are two good ways to persist data in iOS: CoreData NSUserDefaults
Sounds like NSUserDefaults would work for you. Look up tutorials for how to save data using NSUserDefaults.
Which way is recommended to call the different links based on the User's Location?
Not sure what you are asking here, if you clarify, I can edit my answer.
Upvotes: 2