NSUserDafults not working properly in iOS7 - iphone

+(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.

Related

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];
}

NSUSERDefaults class HAVE PROBLEM OR NOT?

NSString *string = #"Hardwork";
NSUserDefaults* defs111 = [NSUserDefaults standardUserDefaults];
[defs111 setObject:string forKey:#"first_name_textfield"];
Hi,
In my code I'm unable to set this value string for key first_name_textfield.
Why is it,so?
I have used this code to save and retrieve the values using NSUserDefaults. So refer this ,it will help you.
IBOutlet UITextField * username;
IBOutlet UITextField * password;
NSString *userNameString;
NSString *passWordString;
userNameString = [[NSString alloc] initWithFormat:[username text]];
[username setText:userNameString];
NSUserDefaults *usernamedefaults = [NSUserDefaults standardUserDefaults];
[usernamedefaults setObject:userNameString forKey:#"StringKey"];
passWordString = [[NSString alloc] initWithFormat:[password text]];
[password setText:passWordString];
NSUserDefaults *passworddefaults = [NSUserDefaults standardUserDefaults];
[passworddefaults setObject:passWordString forKey:#"StringKey1"];
Thanks.

how and where should I set and load NSUserDefaults in a utility app?

I have followed directions in several books and suggestions on some forums but I have issues with my app crashing when I try and set user preferences. I have the following lines on my "done" method in my flipscreenViewController:
- (IBAction)done
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:musicOnOff.on forKey:kMusicPreference];
[userDefaults setObject:trackSelection forKey:kTrackPreference];
[self.delegate flipsideViewControllerDidFinish:self];
}
And the following methods in my mainViewController:
-(void)initialDefaults
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:YES forKey:kMusicPreference];
[userDefaults setObject:#"Infinity" forKey:kTrackPreference];
}
-(void) setvaluesFromPreferences
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL musicSelection = [userDefaults boolForKey:kMusicPreference];
NSString *trackSelection = [userDefaults objectForKey:kTrackPreference];
if(musicSelection == YES)
{
if([trackSelection isEqualToString:#"Infinity"])
song = [[BGMusic alloc]initWithPath:
[[NSBundle mainBundle] pathForResource:
#"Infinity" ofType:#"m4a"]];
else if([trackSelection isEqualToString:#"Energy"])
song = [[BGMusic alloc]initWithPath:
[[NSBundle mainBundle] pathForResource:
#"Energy" ofType:#"m4a"]];
else if([trackSelection isEqualToString: #"Enforcer"])
song = [[BGMusic alloc]initWithPath:
[[NSBundle mainBundle] pathForResource:
#"Enforcer" ofType:#"m4a"]];
else if([trackSelection isEqualToString: #"Continuum"])
song = [[BGMusic alloc]initWithPath:
[[NSBundle mainBundle] pathForResource:
#"Continuum" ofType:#"m4a"]];
else if([trackSelection isEqualToString: #"Pursuit"])
song = [[BGMusic alloc]initWithPath:
[[NSBundle mainBundle] pathForResource:
#"Pursuit" ofType:#"m4a"]];
[song setRepeat:YES];
counter = 0;
}
else
[song close];
}
If anyone out there could please help me see what I am doing wrong it would be much appreciated.
Chuck
You should save your preferences whenever it makes sense to do so in the context of your application. If your code is crashing then you need to acquaint yourself with the debugger and find out why. If you want help with that, then you need to provide a stack crawl at the very least.