I use this line of code to change the language in my app
[[NSUserDefaults standardUserDefaults]
setObject:[NSArray arrayWithObjects:#"en", #"fr", nil]
forKey:#"AppleLanguages"];
Changes won't take place before the user restarts the app.
Do the user really need to restart the app before the changes will take place,
or is it possible to just reload the view controllers in some way to make the app change its language immediately.
write below method in your appDelegate class
+(void)GetLangKey:(NSString *)Langkey
{
NSString *tmpstr=[NSString stringWithFormat:#"%#",[[NSBundle mainBundle]pathForResource:#"LanguageResources" ofType:#"bundle"]];
tmpstr =[tmpstr stringByAppendingString:#"/"];
tmpstr=[tmpstr stringByAppendingString:Langkey];
tmpstr =[tmpstr stringByAppendingString:#".lproj"];
// NSLog(#"%#",tmpstr);
myLocalizedBundle=[NSBundle bundleWithPath:tmpstr];
}
and call this method when your language change
if([language isEqualToString:#"English"])
{
[app_obj GetLangKey:#"en"];
}
else
{
[app_obj GetLangKey:#"fr"];
}
Related
I make phone call from my application using:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"telprompt://XXXXXXXXXX"]];
When the user ends the call, the default Apple-provided Phone app goes to the background and my application resumes focus. This happens automatically.
Now here's what I want: I'd like to call a method every time (and only when) the user returns from a call.
I tried calling this method from applicationWillEnterForeground: or applicationDidBecomeActive: but these callbacks are fired at other times when the application is being launched from the background state(which I dont want).
I'd like to determine if my application is being launched from the background state or if it is returning from a phone call so I can perform a certain task only in the former case and not latter. Any ideas?
----EDIT----
Here's how I finally did it:
See: CallStateDisconnected only detected for incoming calls, not for calls made from my app
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self listenForCalls];
}
- (void)listenForCalls {
self.callCenter = [[CTCallCenter alloc] init];
self.callCenter.callEventHandler = ^(CTCall* myCall) {
NSString *call = myCall.callState;
if([call isEqualToString:CTCallStateDialing]) {
//do ur stuff
}
};
}
You Can use telephony framework, Which provides you call states to determine the state of phone.
You can find out detail from here:
http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Reference/CTCall/Reference/Reference.html#//apple_ref/doc/uid/TP40009590
Why not save a flag indicating that your app sent the user to the phone call. When your applications becomes active, if the flag is set, do the return from phone call method.
ex.
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"DidStartPhoneCall"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"telprompt://XXXXXXXXXX"]];
Then something like,
-(void)applicationDidBecomeActive {
BOOL activeFromCall = [[NSUserDefaults standardUserDefaults] objectForKey:"DidStartPhoneCall"]
if(activeFromCall && [activeFromCall boolValue] == YES) {
// do your method
}
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:#"DidStartPhoneCall"]; // reste flag
}
I have developed a application in which there is a update page which is open only when the application is installed in the device and application run for the first time.After that update page is not shown.
How can I perform this work.
What you have to do is take one bool variable in nsuserdefaults and when app will first launch it will set it to no.After that when you have shown your download page set that bool variable to yes.
Now every time when your app will get opened put a check mark that if your that bool variable is yes so dont show your download page or else what you what to do.
fIRST TIME YOU NEED TO DO:-
NSUserDefaults *std3Defaults=[NSUserDefaults standardUserDefaults];
[std3Defaults setBool:YES forKey:#"update"];
Next time you need to check:-
IN viewdidload of downlaod page:-
NSUserDefaults *std3Defaults=[NSUserDefaults standardUserDefaults];
BOOL check=[std3Defaults boolForKey:#"update"];
if (check==YES) {
//dont show update page
}
else
{
//show update page
}
Use userdefaults
if ([[NSUserDefaults standardUserDefaults] valueForKey:#"Update"]==nil)
{
[[NSUserDefaults standardUserDefaults] setObject:#"YES" forKey:#"Update"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
At first time only it will go inside if condition after that this condition always false
I'm working on an iPhone app, and I'm wondering if I could run some code segment for only once (in other words: an initialization code, that I want it to be executed only at the very first run).
Here's my code, that I execute it at didFinishLaunchingwithOptions method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the tab bar controller's view to the window and display.
[self.window addSubview:tabBarController.view];
[self.tabBarController setSelectedIndex:2];
[self.window makeKeyAndVisible];
[self createPlist1];
[self createPlist2];
[self createPlist3];
return YES;
}
I want the last three messages to be executed only at the very first run. I thought I could use the UserDefaults and set a key after these messages executes (at the first run) and check for the value of that key at each run, but I'm feeling that there's a better idea -which I don't know.
Thanks in advance.
Using a setting (via NSUserDefaults) is how it's normally done. For added benefit, give the setting the meaning of "last run version"; this way, you'll get a chance to run code not only once per app lifetime, but also once per version upgrade.
That said, your run-once code has persistent side effects, right? Those plists go somewhere probably. So you can check if they exist before creating them. Use the result of the run-once code as a trigger for running it again.
EDIT:
NSUserDefaults *Def = [NSUserDefaults standardUserDefaults];
NSString *Ver = [Def stringForKey:#"Version"];
NSString *CurVer = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
if(Ver == nil || [Ver compare:CurVer] != 0)
{
if(Ver == nil)
{
//Run once per lifetime code
}
//Run once-per-upgrade code, if any
[Def setObject:CurVer forKey:#"Version"];
}
A much simpler possible solution ->
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FirstTimeBool"]==nil)
{
[defaults setObject:#"YES" forKey:#"FirstTimeBool"];
... //Code to be executed only once until user deletes the app!
...
this is what I used:
static dispatch_once_t once;
dispatch_once(&once, ^ {
// run once code goes here
});
I think you're on the right track with the User Defaults, something like:
-(BOOL)isInitialRun
{
NSNumber *bRun = [[NSUserDefaults standardUserDefaults] valueForKey:#"initialRun"];
if (!bRun) { return YES; }
return [bRun boolValue];
}
-(void)setIsInitialRun:(BOOL)value
{
[[NSUserDefaults standardUserDefaults] setBool:value forKey:#"initialRun"];
}
Then in your app delegate:
if ([self isInitialRun])
{
[self createPlist1];
[self createPlist2];
[self createPlist3];
[self setIsInitialRun:NO];
}
To my knowledge, the way you propose is the only option. Save a key to NSUserDefaults after you ran it for the first time and check for the existence of said key.
You could however, also check in each of your functions (the createPlist1 - 3 functions) run a check if the PList is already there. Would be a bit cleaner.
One thing I would add to #Seva Alekseyev answer:
After you make any changes (i.e. [Def setObject:CurVer forKey:#"Version"];) you should call [Def synchronize]
I had a problem where changes made to NSUserDefaults using setObject were not getting saved, until I used synchronize.
I've been told that I need to use the registerDefaults: to load the NSUserDefaults if the user has never changed the app settings.
In my AppDelegate I am using the following code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Load default defaults
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Defaults" ofType:#"plist"]]];
[[NSUserDefaults standardUserDefaults] synchronize];
// Add the view controller's view to the window and display.
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
I copied the Root.plist of my settings bundle to the ressources folder of my project and renamed it Defaults.plist
Now in the viewDidLoad method of my view controller i'm using the following code to load the settings:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ((([[defaults objectForKey:kToggleSwitch] isEqualToString:#"Enabled"]) ? YES : NO)) {
//Do Stuff
//BTW i'm using strings for the True/False the values of the ON/OFF positions of the UISwitch
}
Even with doing all this, my default settings stored in the Root.plist of my settings bundle don't get loaded. The only way they load is if the user actually goes into the iPhone settings and simply views my app's settings page.
Clearly i'm doing something wrong here can someone help me out. Btw i'm running it using iOS 4.1
when I made my app, i never knew of any defaults to be registered, so I made a poor man's defaults:
I have a bool variable: haveConfig, which is checked when the settings are loaded:
haveConfig = [prefs boolForKey:#"haveConfig"];
if (!haveConfig)
{
return;
}
/* Load settings here */
....
First time the "haveConfig" will be false, so I don't load them. When the user first changes one of the settings, they are stored together with the "haveConfig" variable:
haveConfig = true;
[prefs setBool:haveConfig forKey:#"haveConfig"];
...
I know it's not perfect, but it works :-)
I have saved data which was saved using NSUserDefaults. I was under the impression that if there was nothing saved to the key already (first time app is run) it would default to 0. This however doesn't seem to be the case.
Here is what I have:
To save:
- (void)viewWillDisappear:(BOOL)animated
{
[[NSUserDefaults standardUserDefaults] setInteger:setbatteryHealthCalculated forKey:#"healthValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
To load:
- (void) viewWillAppear:(BOOL)animated
{
NSInteger setbatteryHealthCalculated = [[NSUserDefaults standardUserDefaults] integerForKey:#"healthValue"];
}
To check for save value:
- (IBAction)check{
NSInteger setbatteryHealthCalculated = [[NSUserDefaults standardUserDefaults] integerForKey:#"healthValue"];
if (setbatteryHealthCalculated = 0) {
[self performSelector:#selector(displayAlert) withObject:nil];
}
}
if (setbatteryHealthCalculated = 0) {
Ehm. Should be
if (setbatteryHealthCalculated == 0)
The comparison operator is == instead of = in C-like languages.
The original code will set setbatteryHealthCalculated to 0 no matter what. Since 0 is false, the if branch will never be executed and no alerts would be shown.
Also, the original code has unbalanced braces.
BTW, is setbatteryHealthCalculated an ivar? If yes, remove the NSInteger before it. Otherwise you're declaring a local variable which shadows the ivar.
Call registerDefaults: during your app's launch to set default values for your defaults (awkward to say, but that's how it work). You'll need to call it before any of the code that accesses your defaults. But NSUserDefaults does return 0 or nil for keys that don't exist.