How to print an alert view only once in my app? - ios

I want to display a message to users if there is a new version of the app they're using, and to display this message at the app's startup, only once.
I've managed to get the app final version and the bundle's one, and comparing them to display or not an alert view to propose the new app. Now I just want to know how to never display this alert again when it has been done once.
Thanks for your help.

To check if you need to display the UIAlertView, store a variable in NSUserDefaults. For instance, in appDidFinishLaunching you could do:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
if (! [defaults boolForKey:#"alertShown"]) {
// display alert...
[defaults setBool:YES forKey:#"alertShown"];
}
In the display alert, just have the code for the alert to pop up.

save the version number in user defaults and check the version with app version, if both are same then continue otherwise show the alert and update the user default variable to latest version. First time leave the user default blank string.

Here is an idea of how to do it.
-(BOOL)application:(UIApplication *)application … {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
if (! [defaults boolForKey:#"notFirstRun"]) {
// display alert...
[defaults setBool:YES forKey:#"notFirstRun"];
}
// rest of initialization ...
}
Here, [defaults boolForKey:#"notFirstRun"] reads a boolean value named notFirstRun
from the config. These values are initialized to NO. So if this value is NO, we execute the if branch and display the alert.
After it's done, we use [defaults setBool:YES forKey:#"notFirstRun"] to change this boolean value to YES, so the if branch will never be executed again (assume the user doesn't delete the app).

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![[defaults valueForKey:#"SHOW_ONLY_ONCE"] isEqualToString:#"1"])
{
[defaults setValue:#"1" forKey:#"SHOW_ONLY_ONCE"];
[defaults synchronize];
}

Related

NSUserDefaults Boolean returns false when using app extension

I am using Notification Extension in my application to change the notification sound.
When the user turns off the UISwitch in the settings page I save in the NSUserDefaults a boolean to keep tracking of the state.
However I have 2 different storyboards for two different languages, each has its own style.
When I am using the storyboard A and I print the state of the boolean in the console, it's printing the state correctly. But when I change the language hence a different storyboard B loads up. The boolean is always returning false.
Although the boolean always return false, I have tested the sound of the notification, it works correctly.
Any idea why the boolean is always returning false? I need the correct value in order to show the state correctly of the ringtone in the settings page.
Here's the code I am using:
- (void)setPlayDefaultSound:(BOOL)playDefaultSound
{
NSUserDefaults *def = [[NSUserDefaults alloc] initWithSuiteName:#"group.com.xx.xxx.NotificationServices"];
[def setBool:playDefaultSound forKey:#"playDefaultSound"];
[def synchronize];
}
- (BOOL)playDefaultSound
{
NSUserDefaults *def = [[NSUserDefaults alloc] initWithSuiteName:#"group.com.xx.xxx.NotificationServices"];
return [def boolForKey:#"playDefaultSound"];
}
And here's a screenshot of the Info.plist of the Notification Services Extension:
You are creating new instances of NSUserDefaults each time. Use the same instance each time.
Use this instead.
- (void)setPlayDefaultSound:(BOOL)playDefaultSound
{
[[NSUserDefaults standardUserDefaults] setBool:playDefaultSound forKey:#"playDefaultSound"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (BOOL)playDefaultSound
{
return [[NSUserDefaults standardUserDefaults] boolForKey:#"playDefaultSound"];
}
There was a mismatch in the name in the capabilities, a typo.
It's working normally now.

Implementing sign in with google (Google oauth) IOS

I have implemented sign in with google i my project, now i don't want user should allow access every time he logs in the project. it should ask for first time only.
I have seen so many solution but don't have any idea how to implement. Please Any body help me
Keep a check at start.
So on your checkViewController viewDidLoad
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
if([defaults boolForKey:#"didLoginGoogle"]) //This returns false by default
{
//Open the page/code for google login
}
else
{
//Open your home page of the app without login
}
Also, once you are done with google login, do a -->
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:true forKey:#"didLoginGoogle"];
[defaults synchronize];
And that it! Using defaults you store the state of your app and then navigate accordingly.

- (void)applicationWillEnterForeground:

maybe someone could assist me.
When my app is launched for the first time a UIAlertController message appears and asks the user if they Need to GoTo the Settings App. using the following code.
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"prefs:root=Wi-Fi"]];
});
Upon return to the App the same UIAlertController Message appears, using this code in the ViewDidLoad:
-(void)viewDidLoad {
if (launched == NO) {
launched = YES;
defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:launched forKey:#"boolKey"];
[defaults synchronize];
My code for the UIAlertController.
} else if (launched == YES) {
[self doSomeThing];
}
}
It appears my Bool Value is not being saved when the settings in the info.plist Application does not run in background: is set to YES, but if the Application does not run in background: is set to NO the else statement is executed. However this is not good because my app is suspended and when launched again I need the original message to appear and it does not, the app is restored to its last state.
Any Suggestions is greatly appreciated.
JZ
1.- Saving into NSUserDefaults does not save anything to the info.plist. NSUserDefaults has its own plist that gets wiped out if you remove your application.
If you want to prevent to launch the same alertview:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults boolForKey:#"boolKey"]) {
defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:#"boolKey"];
My code for the UIAlertController.
}
else {
//Whatever you need to do if its not first launch
}
Now next time you hit your viewDidLoad, since the boolForKey:#"boolKey" has a YES, you won't hit that code and the alertView won't get presented.

How to know a meteor app is started for the first time?

I'd like to prominently show a 'help' button when my meteor app is opened for the first time (on iOS). What's the best way to achieve this? That is, how can I easily confirm my meteor app is opened for the first time, after installing the app on iOS.
set a NSUserDefaults parameter called helpShown, check setting to know which display controller to display. set to true when help is clicked / dismissed
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// set here a bool value
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:#"showHelp"];
//...............
return YES;
}
//somewhere you add your help button
//......
//remove/hide help button
- (void)helpFinishAction:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:#"showHelp"]) {
[defaults setBool:NO forKey:#"showHelp"];
NSLog(#"remove/hide you help button here");
//remove/hide you help button here
}
}

change view on - (void)applicationWillEnterForeground:(UIApplication *)application

How can I change to another view when the app is opened again.
I was thinking of using the method ' - (void)applicationWillEnterForeground:(UIApplication *)application ' in my app delegate and performing a segue maybe? How would I go about doing this correctly?
Thanks guys!
u can use NSUserDeafult to add the status of application when its go in foreground and when application opened again means in "application didFinishLaunchingWithOptions" just check that NSUserDeafult's value and change ur view.
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[prefs setObject:#"first" forKey:#"state"];
[prefs synchronize];
use this code when application go in foreground
and when its come back just
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *state = [prefs stringForKey:#"state"];
now according to state do whatever u want

Resources