We want to have application specific locale regardless of device locale in iOS app. We want to let the user select their desired locale for the app in app login/settings screen. Once user selects any locale the app data will be updated automatically with the locale regardless of device locale.
We found by setting the AppleLanguages in NSUserDefaults can update the current locale for the app:
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:selectedLanguage, nil] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
However, This becomes effective once we kill & restart the app.
Another way I found is changing the resource folder as per the locale. I found two issues with this approach:
In the whole app we have to replace the code related to NSLocalizedString with the code to fetch strings using [NSBundle localizedStringForKey]
Keyboard will be shown as per the device locale.
Is there any approach by using it we can allow the app specific locale selection?
Related
I'm writing a feature of the language change. First screen of the app I'm writing is about setting the language. When he user presses the button, my app directs user to app settings. I expect user to set some language and show it to them as soon as they go back to my app.
But as soon as I click on some language option, my app xcode console says:
Message from debugger: Terminated due to signal 9
Additionally, as soon as I return to my app, it gets loaded as if I closed and reopened it.
I thought that maybe I can have some code that will set a global variables like "currentLanguage" and "previousLanguage" and if those are different - I will load localisation files for "currentLanguage" and will start downloading localised videos from my server.
System terminate your app when user change language in Settings. So you can use NSUserDefaults for persistent storage and compare this values on app start, to detect if language was changed.
Example:
NSString *currentLanguage = [[NSLocale preferredLanguages] firstObject];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject: currentLanguage forKey:#"previousLanguage"];
NSString *previousLanguage = [userDefaults objectForKey:"previousLanguage"];
I want to change the main language just for my app (not the whole phone). When the user selects spanish in the settings menu the whole app should from now be in spanish. I searched a lot and found everytime that the solution is to override the AppleLanguages key in NSUserDefaults. So I made this:
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"es", #"de", #"en", #"ru", #"it", nil] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
When I restart the app I do a log in the AppDelegate and the AppleLanguages is set to the default order (Spanish not the first language what it should be) and the global app language is set to default. Is this the right way?
I was searching for a way to change the iOS device Locale without having to go through the settings and importing some new framework. Just a simple script or so would be great, or even a piece of code. I was wondering if there was something like :
[[NSLocale currentLocale] setLocale:#"WhateverLocale"];
This may or may not fulfill your needs, but one way to gain programmatic access over the app's current locale is to override the AppleLanguages key in NSUserDefaults. For example, to change to French:
[[NSUserDefaults standardUserDefaults] setObject:#[ #"fr" ]
forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
The downside to this approach is that you need to close and reopen the app for the change to take effect.
Note, to return to the system default, simply call:
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"AppleLanguages"];
I need to add one piece of functionality which relates to local storage when the app loads the first time after its downloaded from the app store. I have developed my app using only using jquerymobile and cordova
What I need is some code to achieve the following
Once the app is downloaded onto the iphone / ipad and its opened the app needs to check for the presence of a local storage value called AppID
If appID value does not exist in local storage the app needs to generate a random AppID that is in the following format xxx-xxx-xxx-xxx-xxx-xxx where the x values can be numbers or characters
The randomly generated AppId then needs to be placed in local storage so that it remains in place even after the iphone is rebooted and is only deleted if the app is deleted from the phone.
On the home page of the App I then need to have the AppID value retrieved from local storage displayed
If anyone can directly me to sample code that achieves the above or has already written code that achieves the above i would be very greatful
Use the following code in delegate class method didFinishLaunchingWithOptions
if ([[NSUserDefaults standardUserDefaults] intForKey:#"appID"]==0)
{
//first time launch
//generate an appID and save
[[NSUserDefaults standardUserDefaults] setInteger:app_id forKey:#"appID"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else
{
//has launched before or app_id has been saved before, retrieve app_id
app_id=[[NSUserDefaults standardUserDefaults] intForKey:#"appID"]
}
As far as I know localStorage in not persistent with ios. ios can delete localStorage whenever it needs memory. You can just use a database like webSQL to save your app_id and check at the beginning of the app that it is set or not and if set,what it's value.
In iOS4, when I save NSUserDefaults, the information gets saved. But then, if the application becomes inactive, and I kill the application (by double clicking the home button and then terminating the app), and launch the application again, the NSUserDefaults are not read.
Now, is this the default behaviour of NSUserDefaults that if the app is terminated due to any of the issues, the next launch will not store the information?
The call I am using to persist the info is:-
[[NSUserDefaults standardUserDefaults] registerDefaults:
[NSDictionary dictionaryWithObjectsAndKeys:
"Some date", #"Validity",
nil]];
after you register defaults add this
[[NSUserDefaults standardUserDefaults] synchronize];
until you synchoronize, the standardUserDefaults gives current/correct values as long as the app is alive.
once the app is terminated, since you didn't sync the standardUserDefaults, the app will read the unsynced values on the next launch.
Example: Think of it like a google doc that you are editing.
the AutoSave feature of google document is equivalent to the nsuserdefaults synchornize. If you close the browser before the changes you made to the document has been autosaved, the next time you load it up, you'll see the old content.
The call you mention to persist the user defaults, registerDefaults, does not actually persist anything to disk. What registerDefaults does is initialize the defaults to use for the application if it can't find any on the disk. You should do this on each application launch, typically in the initialize method for the app delegate.
Once your application is running it will typically modify the user defaults. Whenever you want to save these modified defaults to the disk you should call:
[[NSUserDefaults standardUserDefaults] synchronize];
After the defaults have been saved they will be loaded automatically on any subsequent application launches.