Why does the preferred languages array contain only one item? - iphone

I use the following code in my app to print the preferred languages at startup:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defaults objectForKey:#"AppleLanguages"];
NSLog(#"%#", languages);
}
The list only contain one item though. Changing the language in the simulator has no effect. The strange thing is that if I start over with a new project the exact same code above prints a full list of languages. How come my app doesn't get the full list of languages? Is there a setting in XCode I might have switched?

I found a solution:
I had most probably played around with the NSUserDefaults and changed the list myself, not knowing it would stick permanently. Removing the app from the simulator and letting XCode upload it again solved the problem. Good to know when running into similar problems.

Related

Keep track of launches bug

I found some threads that discuss this and implemented it into my code, however I'm having an error.
I am trying to do something every 5th launch.
Also the code sets launchAmounts, is that built in into userDefaults, or do I have to declare this somewhere?
I am doing this from viewwillappear in my main view controller.
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
NSInteger appLaunchAmounts = [userDefaults integerForKey:#"LaunchAmounts"];
appLaunchAmounts = appLaunchAmounts %5;
NSLog(#"app has been launched = %d", appLaunchAmounts);
[userDefaults setInteger:appLaunchAmounts+1 forKey:#"LaunchAmounts"];
if (appLaunchAmounts==0) {
That code looks like it should work, except for the fact that you have it in viewWillAppear. That method could be called many times in one run if you're switching back and forth between different view controllers. You should put it in the applicationDidFinishLaunching method in the app delegate.
What's not working the way you have it now?

Alternative to InAppSettingsKit

Is there an alternative to InAppSettingsKit that is simpler? I find myself needing only 20% of what it offers.
How about ESCOZ's QuickDialog library? Seems like a reasonable alternative.
Well, one alternative is to just build your own settings panel with a regular UIViewController and some buttons and switches, etc. and then save the settings using NSUserDefaults, e.g.
- (IBAction)mySettingSwitchAction:(UISwitch *)theSwitch
{
//save the switch setting
[[NSUserDefaults standardUserDefaults] setBool:theSwitch.on forKey:#"myPreferenceName"];
}
then you can load it again anywhere in your app using
BOOL theValueISet = [[NSUserDefaults standardUserDefaults] boolForKey:#"myPreferenceName"];
Values you set in NSUserDefaults are persistent so if the app is closed and opened again they retain their values. You can call synchronize on NSUserDefaults to force it to save/load the values but this happens automatically on app open/close anyway.

Actions on application startup

I'd like that each time my app starts up (possibly even when it's restored from background) it makes an action (for example TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];).
I'd also like that this action (on startup) can be disabled by a switch in the setting bundle of the app. What should I do? Thanks for your attention.
p.s. I apologize if I used incorrect words.. I'm a beginner :)
There are several methods which you can implement in your application delegate which are available in the UIApplicationDelegate protocol.
When your app is first launched applicationDidFinishLaunching is called.
When your app is restored from the background applicationWillEnterForeground is called.
A switch which you add to your setting bundle will have a key, which is an NSString, associated with it. A switch stores a boolean value encoded as an NSNumber in the standard NSUserDefaults under that key. You can read the value of the boolean from the standard user defaults and use it to determine whether to perform the action.
Apples documentation on how to add a settings bundle is here.
In your settings bundle you'll need a toggle switch. The key that you will look up in the standard user defaults is specified by the Key field. The default value for your toggle switch is specified by the DefaultValue field. See here
Here is what you need to do in your applicationDidFinishLaunching method
static NSString *const kTakeActionOnLaunchSettingKey = #"Key";
- (void)applicationDidFinishLaunching
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL doTakeActionOnLaunch = [userDefaults boolForKey:kTakeActionOnLaunchSettingKey];
if (doTakeActionOnLaunch) {
// Do something
}
}
Initialise it in voidDidLoad {} method. For disabling it you can use switch from object libary
It's easy and I used it to make a cool transition from the splash screen to the home page.
You need to put your code inside the AppDelegate m file.
use
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
to run code at startup.
use the following methods to manage the Backgroud <-> Foreground transitions:
- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
- (void)applicationWillEnterForeground:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application
Hope this helps you.

How to read settings from NSUserDefaults when using inappsettingskit

I am new to XCode and iOS programming. So please assume nothing ...
I am trying to incorporate the inappsettings bundle into my project. It is the 3rd party product found at http://inappsettingskit.com/
I have included all (I think) the files from the kit as well as their settings.app bundle. Through IB, I have built a tab bar where each tab item is a navigational controller. The view of my "Settings" tab is a view controller of class IASKAppSettingsViewController.
When I run my app, the default setting screen included in the kit displays and everything seems to run correctly.
However, I cannot figure out how to actually use the settings that I select in my app. I believe that the settings are stored in [NSUserDefaults standardUserDefaults], but when I query this as below, NSString* s always comes back as nil.
NSUserDefaults* d = [NSUserDefaults standardUserDefaults];
NSString* s = [d objectForKey:#"ROOKIE"];
I have also tried initializing [[NSUserDefaults standardUserDefaults] as follows:
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Root~iphone.InApp" ofType:#"plist"]]];
I have added the key "ROOKIE" to the Root~iphone.InApp.plist file included in the Settings bundles. I put "ROOKIE" in the "key" column and some string value in the "value" column.
Maybe here is where I am doing something wrong? I don't really understand how the .plist files work and if I am accessing the correct one, or if I am doing it in the right way. I have played around and I can't seem to extract any value from any .plist file so it would seem it's my approach rather than the file?
Any insight would be appreciated.

iPhone app using different language than the one set in OS / device?

Is it possible to have my app running in a different language than the one that is set in the OS? I want to have a language switch in my app’s setting menu, where the user can e.g. select german for the app, while his system is running in english.
From what I’ve already read, it seems it’s not possible without having my own localization mechanism…
What do you think?
it can be done easily although it took me weeks to work out how ;-)
I have an iPhone app called iDEX which is a Romanian dictionary. On the iPad, Romanian is not available as a UI language, so I wanted to give the user the option of selecting Romanian in the application settings, as the device settings do not include it.
What you have to do is internationalize the app and localize it as usual. Then, in the main method, set the AppleLanguages key of the standard NSUserDefaults to the language and country of your choice, like so
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSArray arrayWithObject:#"ro_RO"] forKey:#"AppleLanguages"];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool drain];
return retVal;
}
It is imperative that you set the AppleLanguages key before you run UIApplicationMain(), because it is at that point that the UIKit framework is loaded and the default language determined.
It also important that you specify both language and country (eg, #"ro_RO" and not just #"ro") otherwise your app might crash when setting the key.
By doing this, all calls to NSBundle that support localization will look for the language that you have programatically established, as opposed to the one set on the device.
Hope that helps...
You need to implement your custom resource loading mechanism to achieve that. For example you will no longer be able to use NSLocalizedString macro or [NSBundle pathForResource:]. You will always have to specify paths including the localization (as in de.lproj/MyStrings.strings instead of just MyStrings.strings). I would suggest against doing this unless absolutely necessary.