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
Related
I have a sign up screen for my app.then it goes to sign in screen.I want that when I am relaunching the app sign up screen should not appear.it should open the sign in view directly.how do I do it?
You can do this by following steps :
1 - When user successfull signs up,then you should set a string value in a global variable and save it using NSUserDefaults. Just like :
// After user successfully signs up..
NSString *userSignUp = #"someValue"; // set it as a global variable....
[[NSUserDefaults standardUserDefaults]
setObject:valueToSave forKey:#"signUpDone"];
2 - Then put a condition in you appDelegate.m class. Inside the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method,just retrieve that value from NSUserDefaults,which you have stored at user sign up. Then on the basis of that retrieved value,set your root view controller. Just like
userSignUp = [[NSUserDefaults standardUserDefaults]
stringForKey:#"SignUpDone"];
if([userSignUp isEqualToString:#"someValue"])
{
// set Home screen as your root view controller...
}
else
{
// set Sign Up screen as your root view controller..
}
I am not quite sure about what you are aiming to do.
Maybe you should try using NSUserDefaults to store that your app was already launched:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:#"alreadyLaunchedApp"];
Then, at launch, you just need to check the boolean and present the right view:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL alreadyLaunchedApp = [default boolForKey:#"alreadyLaunchedApp"]
Use NSUserDefaults to track that app is first time launch or second time and redirect them according to the state.
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"];
}
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 :-)
Will this be done on every launch of the app? Since it is in the applicationDidFinishLaunching method. Is there a way to do this once and for all?
Thanks!
Hi you can take advantage of preferences..
here is the code to determine application is running for first time or not:(check this in applicationDidFinishLaunching method.)
NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
if([defaults objectForKey:#"Remote_Notification_Registered"] == nil)
{
// This is first run ...
// Do your only one time initialization
[defaults setValue:#"1" forKey:#"Remote_Notification_Registered"];
}
else
{
// Not first run
}
Hope this will help.