Reputation: 705
I am building one Application and need to enter the access credentials (Org code, accesscode) before opening the application. when the user enter the access permissions for the first time then we need to save the preferences. When the user open the app again, automatically he need to continue with application but not with access credentials.
There are no errors in log cat but preferences are not saving. I need to save the preferences and load when the application opens by the user. Am I doing anything wrong in the code?
Upvotes: 1
Views: 1149
Reputation: 15147
This common function is to add your data to NSUserDefaults.. Look this is Class functions so You have to call this using your class name.. Write this in your AppDelegate implementation file..
+(void)addToNSUserDefaults:(id)pObject forKey:(NSString*)pForKey
{
NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
[defaults setObject:pObject forKey:pForKey];
[defaults synchronize];
}
This is to get data from NSUserDefaults..
+(id)getFromNSUserDefaults:(NSString*)pForKey
{
id pReturnObject;
NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
pReturnObject = [defaults valueForKey:pForKey];
return pReturnObject;
}
When you start application, Just Check this in this method of AppDelegate,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSString *strtmp = [AppDelegate getFromNSUserDefaults:YOUR_KEY];
if(strtmp == nil)
{
//User have to enter data to save in User Defaults
}
else
{
//Application Starts without entering Code, as you have it in your UserDefaults
}
}
Upvotes: 5