Question

I have an application with an alertview that shows up when the application launches to explain about the app function. On my alertView I want to make "Don't show again" button, so the user doesn't have to see the same alert every time he/she uses my app. So how can I stop the alertView from showing up after the user selects this button. Should I work on the appDelegate or should I work on the viewcontroller where my alertview will pop up?

Was it helpful?

Solution

I would store a value in NSDefault for this, as we cannot change this on UIAlertView.

So once the UIAlertView has shown, set this value to something that represents "read", retrieve it in one of the App Delegate methods such as applicationDidBecomeActive and use it as a condition in a if statement for displaying the UIAlertView.

Let say you had an NSInteger = 0, which signifies "unread", once the UIAlertView is shown, set it to 1 and store it in NSDefault.

if(alertHasBeenRead == 0)
{
   //bring up alert view
}

And subsequently in one of the delegate callback, set this value to something else other than 0, for example.

Hope this helps.

OTHER TIPS

Try this:

if (![@"1" isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"alert"]]){


    UIAlertController *alert = [UIAlertController
                                alertControllerWithTitle:NSLocalizedString(@"Information", @"The title of an alert.")
                                message:NSLocalizedString(@"Some text goes here.", @"The message of an alert.")
                                preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *ok = [UIAlertAction
                         actionWithTitle:NSLocalizedString(@"OK", @"A common affirmative action title, like 'OK' in english.")
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {

                         }];
    UIAlertAction *dontshowagain = [UIAlertAction
                                    actionWithTitle:NSLocalizedString(@"Don't Show Again", @"A common decline action title, like 'NO' in english.")
                                    style:UIAlertActionStyleDefault
                                    handler:^(UIAlertAction * action)
                                    {
                                        [[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"alert"];
                                        [[NSUserDefaults standardUserDefaults] synchronize];

                                    }];

    [alert addAction:ok];
    [alert addAction:dontshowagain];

    [self presentViewController:alert animated:YES completion:nil];

Let me know if this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top