Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying alert from app delegate before displaying alert from viewDidload

I am attempting to display the message contained within a push notification through the app delegate as outlined in the parse.com documentation.

The problem I am having is that in my viewdidload method for my first view controller, i am presenting an alert which the user MUST see before they use the app.

How can I call the method from my app delegate after the user sees the Alert from the viewdidload method?

EDIT:

So i have, as suggested in the comments, added a global Variable which i set to true once i have Displayed the alert from my ViewDidload method, but the Notification Alert from my appDelegate still does not appear.

here is my app delegate.m file:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [Parse setApplicationId:@"xxxxxxxxxxxxxxxx"
                  clientKey:@"xxxxxxxxxxxx"];

    // Register for Push Notitications, if running iOS 8
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                        UIUserNotificationTypeBadge |
                                                        UIUserNotificationTypeSound);
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                                 categories:nil];
        [application registerUserNotificationSettings:settings];
        [application registerForRemoteNotifications];
    } else {
        // Register for Push Notifications before iOS 8
        [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                         UIRemoteNotificationTypeAlert |
                                                         UIRemoteNotificationTypeSound)];
    }
    return YES;



    NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];


    if (Notification == true) {
        if (![pushText  isEqual: @""]) {
            pushText = [[notificationPayload objectForKey:@"aps"] objectForKey:@"alert"];
            UIAlertView *alert_news = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"News", "")
                                                                 message:pushText
                                                                delegate:nil
                                                       cancelButtonTitle:@"Ok"
                                                       otherButtonTitles: nil];
            [alert_news show];


            }
    }


}

And here is my viewdidload method:

 RoadSafetyAppAppDelegate *AppDelegate;

- (void)viewDidLoad
{
        AppDelegate = (RoadSafetyAppAppDelegate *)[[UIApplication sharedApplication] delegate];
        [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.



    backgroundImage.alpha = 0.3;
    toRecipients = [[NSArray alloc]initWithObjects:@"[email protected]", nil];
    static int appCounter;
    if ( appCounter < 1   ) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Disclaimer", "")
                                                        message:NSLocalizedString(@"Using a mobile phone whilst driving is against the law. Ensure that you are not behind the wheel when using this app.", "")
                                                       delegate:nil
                                              cancelButtonTitle:@"I agree to not use a mobile phone while driving"
                                              otherButtonTitles: nil];
        [alert show];
        appCounter = appCounter+1;

       AppDelegate.NotificationAlert = @"1";
        AppDelegate.Notification = true;



    }

}
like image 226
scb998 Avatar asked Dec 02 '14 01:12

scb998


Video Answer


2 Answers

since you want to show the disclaimer ONE time and to be sure that the user saw it and TAPED on Agree Button before showing any notification. you can do that using a simple local notification.

in delegate (...didFinishLaunchingWithOptions:)

 -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

        //......you code here
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"disclaimerShown"]==nil)

            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"disclaimerShown"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }


         //......you code here

        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"disclaimerShown"]){ //YES

                    if (![pushText  isEqual: @""]) {
                    pushText = [[notificationPayload objectForKey:@"aps"] objectForKey:@"alert"];
                    UIAlertView *alert_news = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"News", "")
                                                                         message:pushText
                                                                        delegate:nil
                                                               cancelButtonTitle:@"Ok"
                                                               otherButtonTitles: nil];
                    [alert_news show];


                    }


          }
}


-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

    NSString *value=[NSString stringWithFormat:@"%@",[notification.userInfo valueForKey:@"key"]];
    if ([value isEqualToString:@"disclaimerShown"]) {
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"disclaimerShown"];
                [[NSUserDefaults standardUserDefaults] synchronize];
     ///continue handle parse.com notification
    }

}

in you ViewController:

-(void)viewDidLoad{
            //...


           if ([[NSUserDefaults standardUserDefaults] boolForKey:@"disclaimerShown"]==NO){

                       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Disclaimer", "")
                                                                    message:NSLocalizedString(@"Using a mobile phone whilst driving is against the law. Ensure that you are not behind the wheel when using this app.", "")
                                                                   delegate:nil
                                                          cancelButtonTitle:@"I agree to not use a mobile phone while driving"
                                                          otherButtonTitles: nil];
                    alert.tag = 1;
                    [alert show];
               }


            //...
}

pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1) {//the disclaimer alert
        if (buttonIndex == 0) {
            UILocalNotification *alarm = [[UILocalNotification alloc] init];
            alarm.userInfo = @{@"key": @"disclaimerShown"};
            alarm.fireDate = [NSDate date];
            alarm.timeZone = [NSTimeZone defaultTimeZone];

            [[UIApplication sharedApplication] scheduleLocalNotification:alarm];
        }
    }

}
like image 91
Idali Avatar answered Sep 30 '22 18:09

Idali


Instead of AppDelegate bool flag property use NSUserDefaults;

In AppDelegate update this line from:

if (Notification == true)

to

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"Notification"] == YES)

And in ViewController -> viewDidLoad method update line from:

AppDelegate.Notification = true;

to

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"Notification"];
[[NSUserDefaults standardUserDefaults] synchronize];

Hope this helps.

like image 37
Mrunal Avatar answered Sep 30 '22 18:09

Mrunal