Faisal Syed
Faisal Syed

Reputation: 931

How to display alert once a day

I'm trying to figure out how I can make an alert popup once the user has reached their goal. However, I only want it to show once a day after it's been triggered. Apologies in advance. if my logic is off with tackling this issue.

// Check to see if hydration goal has been reached
if (waterCups >= dailyHydrationGoal) {

   // User has reached goal

    if (alert has not been fired yet today) {
        // alert pop up you reached your goal!
    }
}

How would I go about ensuring it only launches once per day?

Upvotes: 3

Views: 1032

Answers (2)

Lal Krishna
Lal Krishna

Reputation: 16200

You can use isDateInToday: method to check whether the given date is in “today.”

NSDate *lastAlertDate = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@"lastAlertDate"];
if(![[NSCalendar currentCalendar] isDateInToday:lastAlertDate]){
     //Show alert
     NSDate *today= [NSDate date];
     [[NSUserDefaults standardUserDefaults] setObject:today forKey:@"lastAlertDate"];
}

Upvotes: 4

Kibitz503
Kibitz503

Reputation: 867

Do you have a persistence store? (NSUserDefaults, core data etc...)

You can store a time stamp (NSDate) of the last time an alert was shown then check if the last time it was shown is today. How to determine if an NSDate is today?

If an alert has not been shown today, pop an alert and record a new time stamp.

Upvotes: 1

Related Questions