How to edit stored value in NSUserDefaults - iphone

I am storing some string value in NSUserDefaults,
In the next button click I need to edit stored value and save new string in the place of old string.
How can I do this.
Can any one please help me.
Thank you in advance.

[[NSUserDefaults standardUserDefaults] setObject: newStringValue forKey: kMyKey];

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"Password"];
[prefs setObject:#"" forKey:#"Password"];
NSString *passwordStr = [prefs stringForKey:#"Password"];
[[NSUserDefaults standardUserDefaults] synchronize];

Related

NSUserDafults not working properly in iOS7

+(NSString *)languageKey{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:#"AppleLanguages"];
NSString *lang = [languages objectAtIndex:0];
if([lang isEqualToString:#"en"])
return #"en";
else if([lang isEqualToString:#"ar"])
return #"ar";
return #"en";
}
+(NSString *)localizedStringForKey:(NSString *)key{
NSString *path;
if([[self languageKey] isEqualToString:#"en"])
path = [[NSBundle mainBundle] pathForResource:#"en" ofType:#"lproj"];
else if([[self languageKey] isEqualToString:#"ar"])
path = [[NSBundle mainBundle] pathForResource:#"ar" ofType:#"lproj"];
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
NSString* str=[languageBundle localizedStringForKey:key value:#"" table:nil];
return str;
}
Sometimes i am getting wrong key here in iOS7 and below version working fine. Is this Xcode6.0 bug?
How to get rid of this issue
Advance Thanks
well Objective-C and Xcode-6 works fine with UserDefaults, in my experience with NSUserDefaults you need to be very careful with the way you save and retrieve data:
I Save data like this:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:#"data" forKey:#"key"];
[prefs synchronize];
And i retrieve data like this:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *theString = [prefs stringForKey:#"key"];
I Also see a [defaults synchronize] when you are retrieving the data, I think that is not necessary because you aren´t saving data, good luck.

Localization in iPhone not working on button click

I made an app for iPhone. In this user can change language on button click. but NSLoalizedString is not converting the value.
the code is
-(IBAction)btn1pressed:(id)sender {
SecViewController *sec = [[SecViewController alloc] initWithNibName:#"SecViewController" bundle:nil];
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
// NSLog(#"%#", [userDefaults objectForKey:#"AppleLanguages"]);
languages =#"en";
[[NSUserDefaults standardUserDefaults] setObject:languages forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"%#", [userDefaults objectForKey:#"AppleLanguages"]);
// NSLog(#"%#", NSLocalizedString(#"Subhash", nil));
[self.navigationController pushViewController:sec animated:YES];
}
-(IBAction)btn2pressed:(id)sender {
SecViewController *sec = [[SecViewController alloc] initWithNibName:#"SecViewController" bundle:nil];
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
// NSLog(#"%#", [userDefaults objectForKey:#"AppleLanguages"]);
languages = #"es";
[[NSUserDefaults standardUserDefaults] setObject:languages forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"%#", [userDefaults objectForKey:#"AppleLanguages"]);
// NSLog(#"%#", NSLocalizedString(#"Subhash", nil));
[self.navigationController pushViewController:sec animated:YES];
}
-(IBAction)btn3pressed:(id)sender {
SecViewController *sec = [[SecViewController alloc] initWithNibName:#"SecViewController" bundle:nil];
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
// NSLog(#"%#", [userDefaults objectForKey:#"AppleLanguages"]);
languages =#"ja";
[[NSUserDefaults standardUserDefaults] setObject:languages forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"%#", [userDefaults objectForKey:#"AppleLanguages"]);
// NSLog(#"%#", NSLocalizedString(#"Subhash", nil));
[self.navigationController pushViewController:sec animated:YES];
}
Try
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:languages,nil] forKey:#"AppleLanguages"];
Use NSLocalizedString(), for localization.
Also give corresponding localization string.

NSUserDefault to NSArray

Icstored the value of 2 label in NSUserDefault, trought NSMutableArray.
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// Add a bookmark
NSMutableDictionary *bookmark = [NSMutableDictionary new];
[bookmark setValue:author.text forKey:#"author"];
[bookmark setValue:book.text forKey:#"book"];
[bookmarks addObject:bookmark];
// Save your (updated) bookmarks
[userDefaults setObject:bookmarks forKey:#"bookmarks"];
[userDefaults synchronize];
Now my problem is how retrieve the values of NSUserDefault trought NSArray?
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
self.dataArray = [prefs arrayForKey:#"bookmarks"];
NSString *author = ??
NSString *book = ??
NSString * author = [self.dataArray objectForKey: #"author"];
NSString * book = [self.dataArray objectForKey: #"book"];
This assumes you successfully saved the array and that you successfully re-loaded the array from NSUserDefaults. You really should do some error checking in your code.
In the dataArray, you can store lots of bookmarks.This methods can log all of the bookmarks.
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
self.dataArray = [prefs objectForKey:#"bookmarks"];
for(int i=0;i<[dataArray count];i++){
NSDictionary *dic = [dataArray objectAtIndex:i];
NSLog(#"%#",dic);
NSString * author = [dic objectForKey: #"author"];
NSString * book = [dic objectForKey: #"book"];
}
you have inserted an array with a dictionary. you can retrieve it by using
self.dataArray = [prefs arrayForKey:#"bookmarks"];
NSString *author = [self.dataArray objectAtIndex:0] objectForKey:#"author"];
NSString *book = [self.dataArray objectAtIndex:0] objectForKey:#"book"];
But it is better to store the dictionary directly in user defaults.

How to get UserName in Facebook API ios?

i'm using Facebook native API for sharing in my iPhone App, i need to know as soon as user logged in i need his username, how can i get this?
Any help is appriciated in advance.
You can use Graph API to do this. Just make a request in fbDidLogin delegate method:
- (void)fbDidLogin {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:#"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:#"FBExpirationDateKey"];
[defaults synchronize];
[facebook requestWithGraphPath: #"me" andDelegate: self];
}
And then you can get all the information about current user in didLoad method:
- (void)request:(FBRequest*)request didLoadRawResponse:(NSData*)data
{
NSString *response = [[NSString alloc] initWithData: data
encoding:NSUTF8StringEncoding];
NSLog(#"%#", response);
[response release];
}
- (void) request:(FBRequest*)request didLoad:(id)result
{
if ([result isKindOfClass:[NSDictionary class]])
{
NSString *name = [result objectForKey: #"name"];
NSString *fbID = [result objectForKey:#"id"];
}
}

How to access values from settings.bundle?

I am doing a project where I have to retrieve some values in settings.bundle. For this I have to retrieve a default value in such a manner.
I am accessing it as under
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
stringObject = [defaults objectForKey:#"Key_Value"];
But when I try to print stringObject
NSLog(#"%#",stringObject);
Then it always print null.
I have saved a string value in the settings.bundle with key "Key_Value". But It returns Null.
tell me where I am doing wrong
check you have stored you value in this way:-
NSUserDefaults *pref3=[NSUserDefaults standardUserDefaults];
[pref3 setObject:*yourstring* forKey:#"Key_Value"];
[NSUserDefaults resetStandardUserDefaults];
your code of retreiving is correct.make sure stringObject is of NSString type.
Call this function will resolve your issue
- (void)registerDefaultsFromSettingsBundle {
// this function writes default settings as settings
NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:#"Settings" ofType:#"bundle"];
if(!settingsBundle) {
NSLog(#"Could not find Settings.bundle");
return;
}
NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:#"Root.plist"]];
NSArray *preferences = [settings objectForKey:#"PreferenceSpecifiers"];
NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];
for(NSDictionary *prefSpecification in preferences) {
NSString *key = [prefSpecification objectForKey:#"Key"];
if(key) {
[defaultsToRegister setObject:[prefSpecification objectForKey:#"DefaultValue"] forKey:key];
NSLog(#"writing as default %# to the key %#",[prefSpecification objectForKey:#"DefaultValue"],key);
}
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];
}