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?
Related
iOS application written in Objective-C only show English & Emoji keyboards.
All other application are working normally and other keyboards (ex: Polish, Hebrew) are available to use.
I tried to look for .plist restrictions or something i did in the development process. didn't find anything.
appreciate your help.
Well, i found the problem.
In my main.m file i had this two rows that prevent my system from allowing any other keyboard then english:
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"en_EN", nil] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize]; //to make the change immediate
To overwrite this code for users who already installed the applkaction - i have changed it to:
if ([[NSUserDefaults standardUserDefaults]valueForKey:#"AppleLanguages"]){
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize]; //to make the change immediate
}
Now everything is working as expected.
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?
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 am a web developer that has recently moved over to iOS dev. I have implemented a login system for my website and I want to integrate this same functionality within my app. I will have login UI in the app that communicates with the web server using the AFNetworking library to make POST requests.
However, I am not sure how I will deal with remembering things in regard to user logins such as whether the user was logged in previously? With Web, I would do cookies and sessions, but I am not sure whether this is the best approach with iOS. I've also read about some people talking about storing things within the iOS keychain for security reasons. Can anyone shine some light on what the best practise for achieving what I want would be?
Thanks!
I would use NSUserDefaults but I think you should use token-based approach for your iOS client. When ever you log in and get a successful response, you should store your token in user defaults with the following code.
[[NSUserDefaults standardUserDefaults] setValue:token forKey:#"auth_token"];
[[NSUserDefaults standardUserDefaults] synchronize];
Your logout function could look something like this.
- (void)logout {
[[NSUserDefaults standardUserDefaults] setValue:nil forKey:#"auth_token"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
You can send this auth_token each time you make a web request so that the server knows those requests are authorized.
In objective-C there is the NSUserDefaults that can be used similarly.
To save a bool value for example you would do the following
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"IsLoggedIn"];
[[NSUserDefaults standardUserDefaults] synchronize];
to later load the value you write
BOOL isLoggedIn = [[NSUserDefaults standardUserDefaults] boolForKey:#"IsLoggedIn"];
Hope it helps!
I have an iOS application that uses the keychain to store some information related to the authentication. I would like to remove this data from the keychain when the application is uninstalled.
How can I get aware of the application being uninstalled/removed?
You canĀ“t detect when your application is going to be deleted from your device. But you could detect when your app is launched for first time, after to be installed. For this I use the next code:
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"HasLaunched"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasLaunched"];
[[NSUserDefaults standardUserDefaults] synchronize];
//Here delete your info kept in your keychain. If not exist will not delete nothing, but if it exist mean your app has been installed again.
[removeYourPropertyInKeychain];
}