OSX preferences file - removePersistentDomainForName has different functionality in 10.8? - objective-c

This code works for one of our developers on 10.7 but not for me on 10.8
working = it deletes the preferences .plist file for the bundle. The dev on 10.7 also has a lockfile whereas I do not. It's not a problem of file access - I tried [resetStandardUserDefault] and that made a new file, but that's not exactly we want to do.
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
I am having trouble locating any info about changes in 10.8 - does this sound familiar to anyone?

I have the same issue and indeed there seems to be no info about any changes, also don't get any errors or warnings. [NSUserDefaults resetStandardUserDefault] doesn't do anything for me. What I do currently is simply overwrite the default values to reset them (give [[NSUserDefaults standardUserDefaults] setValuesForKeysWithDictionary:
[NSDictionary dictionaryWithObjectsAndKeys:...]] the same input as originally given to [[NSUserDefaults standardUserDefaults] registerDefaults:
[NSDictionary dictionaryWithObjectsAndKeys:...]].
Not really an answer, but I don't have enough rep to comment..

Related

How to change application language runtime without restarting application in ios with objective c?

currently I am using the code below to change the application language.
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects: #"French", nil] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
but it works on restarting application.
I want to change application language on selecting from drop down without restarting application.
Just use this library to change the language, on the go!
set your language text using NSLocalizedString(<#key#>, <#comment#>)
and push again same viewController

How can I set my iOS device to have a locale of "en" instead of "en_US"?

According to this question here:
Looking for a list of all available languages in iOS
There are numerous locales that have only a language code with no region code. Can anyone explain to me how to set my device into a state whereby the value of
[[NSLocale currentLocale] localeIdentifier]
is #"en" and not #"en_US"? I am trying to track down a bug and I believe it may be related to this state, which I cannot reproduce on my device or the simulator.
Note - I am aware that "en" is a language and not locale -- I am trying to reproduce a possible bug.
Reading a different question/answer here on StackOverflow you can force the application to use a specific language:
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"en", #"en", #"en_AU", nil] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize]; //to make the change immediate

NSUserDefaults won't delete object for key

When the user logs in to my application, I download a token from my JSON server and store it in NSUserDefaults, as such:
[[NSUserDefaults standardUserDefaults] setValue:token forKey:TOKEN];
When the user logs out in a subsequent page, I call this method and return back to the login screen:
[[NSUserDefaults standardUserDefaults] setObject:#"" forKey:TOKEN];
(and before that I called [[NSUserDefaults standardUserDefaults] removeObjectForKey:TOKEN];)
It doesn't matter how I try to delete this user defaults, whenever I load up my app, it always shows me the full token and not an empty string nor a null value.
When reading around, apparently it has something to do with read write cycles? But even if i leave it for a while, the key still remains. Is this a simulator problem?
Whatever the cause, how do i get around this?
It's the simulator problem of caching the memory first. It only happens in xcode and should not happen on a device.
Do you call
[[NSUserDefaults standardUserDefaults] synchronize];
after the key was deleted? Maybe you are not persisting the changes into the database.

How to force localization at compileTime or better at runtime

i've got a IOS project with many different images to display depending on the language, and i can't find a way to force loading the correct image at runtime (not switching a language to another).
Actually it seems that the files are loaded before the AppDelegate get a chance to interfer.
if i do in appDelegate (didFinishLaunchingWithOptions:):
[[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects: #"es", #"en", nil] forKey:#"AppleLanguages"];
It's already too late, image are already loaded in the wrong language. So in order to accomplish this i have moved the NSUserDefaults into the main() call, and this tricks works. But my questions are: Do Apple will reject my app? And is there a better, clean approach to synchronize a language at compile time (or with plist or something like that)?
Update
it seems that just replacing [[NSUserDefaults standardUserDefaults] setObject: forKey:], further step before calling my nib file did the trick. It's like [NSUserDefaults standardUserDefaults] is called asynchronously. So maybe the setter method was actually evaluated after my nib file was loaded. But can i change the localizable setting via info.plist ?
You can localize the Info.plist (InfoPlist.strings), which lets you set the Default.png for each language.

AppDelegate int value keeps getting reset

In my project AppDelegate file, I have an int declared called correctAnswersCountR1. So in the app they take a small quiz and that variable keeps track of how many correct answers they got. Now somewhere else in the project I use this variable like so:
int r1score=appDelegate.correctAnswersCountR1;
The problem is that apparently if I exit the app and come back, the value isn't remembered, and is set back to its default value. How can I store this number so that it is remembered if the user closes the app and comes back?
You can use NSUserDefaults to store your value:
**Saving**
//Do this right before the app exits
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSInteger
[prefs setInteger:42 forKey:#"integerKey"];
// This is suggested to synch prefs, but is not needed
[prefs synchronize];
**Retrieving**
//Do this when your app is loaded again
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSInteger
NSInteger myInt = [prefs integerForKey:#"integerKey"];
You could set up core data for your project, though unless you plan on storing more information persistently for your app Core Data might be a bit of overkill. You could also try writing it to a file and just reading it on load.
Edit: For this specific scenario Oscar's answer seems more appropriate, though depending on where you plan on going with your app, CoreData or a file may be a good choice.
You'll want to do some research about NSUserDefaults.
Here is the apple documentation
On another note it's not considered best practice to store data in the AppDelegate
You have to use a preferences storing mechanism. I'm not all too familiar with the iPhone toolkit, but on Mac OS X and Cocoa there's NSUserDefaults. When your app launches, you would load the value from the user defaults (preferences) through [[NSUserDefaults standardUserDefaults] intForKey:#"count"], and whenever the value changes, you would call [[NSUserDefaults standardUserDefaults] setInt:answerCount... forKey:#"count"].
I'm pretty sure that you want to write the value into a file to make it persistent.
This is because an application which moved to the background might be terminated for example if the system measures out that it needs some more memory.
So it kills the application and restarts it when the user switches over.