- (void)applicationWillEnterForeground: - ios

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.

Related

How to hide buttons, labels, etc for entire time iOS app is running, then show them again at start?

I'm coding an iOS app in Objective C. I want the terms and conditions to appear when a user opens the app. I'm going to have a "don't show these again" button, which is all figured out using NSUserDefaults. What I can't figure out is how to make the terms and conditions disappear if the user hits "agree" (termsPressed) but NOT "don't show these again" (neverAgainPressed). I can hide them using button.hidden, but then as soon as the user returns to the main screen of the app the terms and conditions appear again, overlaying the main screen just as they did when the app first launched.
I've tried setting an NSUserDefault when "agree" is pressed, then resetting it when -applicationWillTerminate is called, but it appears that applicationWillTerminate is not called reliably when the app closes if it's closed from the background, and hence the user would never see the terms again even if they hadn't hit "don't show these again". Here's my code:
- (void)viewDidLoad {
// Hide all the terms and conditions elements if either "neverAgain" or "termsPressed" is true
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL neverAgain = [defaults boolForKey:#"neverAgain"];
if (neverAgain == TRUE){
_terms.hidden=YES;
_hideTerms.hidden=YES;
_background.hidden=YES;
_dontShow.hidden=YES;
}
BOOL termsPressed = [defaults boolForKey:#"termsPressed"];
if (termsPressed == TRUE){
_terms.hidden=YES;
_hideTerms.hidden=YES;
_background.hidden=YES;
_dontShow.hidden=YES;
}
NSLog(#"View loaded. termsPressed = %i", termsPressed);
NSLog(#"View loaded. neverAgain = %i", neverAgain);
[super viewDidLoad];
}
- (IBAction)neverAgainPressed:(id)sender {
BOOL neverAgain = TRUE;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:neverAgain forKey:#"neverAgain"];
[defaults synchronize];
NSLog(#"Don't show terms again pressed, neverAgain set to TRUE");
}
- (IBAction)termsPressed:(id)sender {
_terms.hidden = YES;
_hideTerms.hidden = YES;
_background.hidden = YES;
_dontShow.hidden = YES;
BOOL termsPressed = TRUE;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:termsPressed forKey:#"termsPressed"];
[defaults synchronize];
NSLog(#"Terms accepted, termsPressed set to TRUE");
}
- (void)applicationWillTerminate:(UIApplication *)application {
BOOL termsPressed = FALSE;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:termsPressed forKey:#"termsPressed"];
[defaults synchronize];
NSLog(#"Application entering background, termsPressed reset to FALSE");
}
#end
If there was a method that was called every time an app quit, or a version of .hidden that lasted the whole time an app was running, I'd be all set. Let me know if you guys have any ideas.
If you want "don't show these again", why reset? You can show at first launch and after some time (30 days for example):
BOOL ranBefore = [[NSUserDefaults standardUserDefaults] boolForKey:#"kRanBefore"];
NSDate *lastDate = [[NSUserDefaults standardUserDefaults] objectForKey:#"kLastCloseDate"];
NSTimeInterval timeDiff = [[NSDate date] timeIntervalSinceDate:lastDate];
int days = timeDiff / 86400;
if ((!ranBefore) || (days > 30)) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"kRanBefore"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:#"kLastCloseDate"];
... show something
}
or reser at
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}

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
}
}

Show alert when ApplicationDidBecomeActive and redirect to another View if user choose Continue

I'm implementing an in-app purchase feature. In addition to Apple Store standard purchase flow, I have another receipt validation from my own server side. Sometimes, the purchase procedure is complete on Apple side and the app exist before my server validates the receipt.
So I store the receipt in NSUserDefaults. And whenever applicationDidBecomeActive, I would check if there's pending receipt in NSUserDefaults. If yes, I would like to pop up an alert, asking user whether to continue completing the purchase. If user canceled, then I would remove the receipt cancel the purchase. Otherwise, I would direct user to a purchase view, and do the rest of work.
Previously, I did the checking inside AppDelegate ApplicationDidBecomeActive. It seems not to be a good practice. I then try to move the code into MainViewController, catch AppicationDidBecomeActive notification in init. But I'm not sure what's the correct way of doing so? I try to catch the event with selector:#selector(resumePurchase:) and here's my resumePurchase code
- (void)resumePurchase:(NSNotification*)notification {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSData *purchaseDocument = (NSData*)[userDefaults objectForKey:#"lastPurchaseDocument"];
if (!purchaseDocument) {
NSString *message = [NSString stringWithFormat:NSLocalizedString(#"You have an incomplete purchase in the app, do you want to continue the payment?",#"resume purchase.")];
self.resumePurchaseAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Resume Payment",#"alter title")
message:message
delegate:self
cancelButtonTitle:NSLocalizedString(#"Cancel",#"Cancel caption")
otherButtonTitles:NSLocalizedString(#"Continue",#"altert approve button"), nil];
[self.resumePurchaseAlert show];
}
}
This seems ok so far. I'm stuck at the Continue button handler
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView == self.resumePurchaseAlert) {
if (buttonIndex == 0) { // Cancel
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults removeObjectForKey:kPurchaseDocument];
[userDefaults synchronize];
}
if (buttonIndex == 1) { // Continue
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSDictionary *purchaseDocument = (NSDictionary*)[userDefaults objectForKey:kPurchaseDocument];
NSData *purchasedReceipt = [purchaseDocument objectForKey:kPurchasedReceipt];
NSString *purchasedFeature = [purchaseDocument objectForKey:kPurchasedFeature];
if (purchasedFeature && purchasedReceipt) {
// here I want to redirect the view to PurchaseViewController
}
}
}
}
I don't know how can I redirect the view to PurchaseViewController in an elegant way...
in other UIViewController check value that you saved in NSUserDefaults and do nabvigation according to it.

Open different view on initial run

I'm trying to make my app launch a different view on the first time it is loaded up. I've got this code at the moment which implements that something should happen when the app is first launched. I've got this code but it lacks the code to open Initialviewviewcontroller. I have no idea how to do this so help would be much appreciated
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL hasRunBefore = [defaults boolForKey:#"FirstRun"];
if (!hasRunBefore) {
[defaults setBool:YES forKey:#"FirstRun"];
[defaults synchronize];
// what goes here??
else
{
NSLog (#"Not the first time this controller has been loaded");
So I should launch a different view controller in the if statement. But what should I put ?
Solution No. 1
I've written a simple snippet for this thing because I use it quite a lot. You can find it here.
Feel free to use it, fork it or modify it!
Solution No. 2
You can do something like this in your AppDelelegate.m
Add this simple method at the bottom:
- (BOOL)hasEverBeenLaunched
{
// A boolean which determines if app has eer been launched
BOOL hasBeenLaunched;
// Testig if application has launched before and if it has to show the home-login screen to login
// to social networks (facebook, Twitter)
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"HasAlreadyLaunched"]) {
// Setting variable to YES because app has been launched before
hasBeenLaunched = YES;
// NSLog(#"App has been already launched");
} else {
// Setting variable to NO because app hasn't been launched before
hasBeenLaunched = NO;
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasAlreadyLaunched"];
[[NSUserDefaults standardUserDefaults] synchronize];
// NSLog(#"This is the first run ever...");
}
return hasBeenLaunched;
}
After implementation of this method you can use it like that:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Determining Storyboard identifier for first view
NSString *storyboardID = [self hasEverBeenLaunched]? #"MainView" : #"LoginView";
// Setting proper view as a rootViewController
self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:storyboardID];
return YES;
}

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

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];
}

Resources