Skip to content

Instantly share code, notes, and snippets.

@dautermann
Created February 17, 2016 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dautermann/baa5e13dd0ad4a1d5313 to your computer and use it in GitHub Desktop.
Save dautermann/baa5e13dd0ad4a1d5313 to your computer and use it in GitHub Desktop.
If you were to show an app rating alert for active users of your app who have used your app at least on 3 unique days in the last 5 days. Please provide code for it.
// The way I would approach this problem would be to save a set of two distinct dates to
// NSUserDefaults and if these three days (the two days from the set plus today's third date) lie within the last five days,
// I'll show the alert. If we have two dates in the set, replace the oldest with the newest/current date.
// I'm using NSSet because it's distinct dates, versus an array where we might have duplicate dates.
// I'm making JetDate as a subclass of NSDate where the time component of NSDate (02-17-2015 00:00:00 GMTz)
// is dropped and only the date is considered
- (BOOL) shouldShowRatingAlert
{
NSMutableSet *dateSet = [[NSUserDefaults standardDefaults] getDateSet]; // getDateSet is a custom method which handles unarchiving NSDates!
JetDate *currentDate = [JetDate date];
// check to make sure today's date isn't already in the date set...
// if so, we either displayed the rating alert or we didn't, but we already looked at this
// if there's less than 2 dates in the dateset, we don't need to show the rating alert
if([dateSet count] < 2)
{
// add today's date
[dateSet addObject: currentDate];
return NO;
}
// if we are here, we have two dates and today's use would be the third date
// calculate the distance between the oldest date and today's date
const NSTimeInterval fiveDaysTimeInterval = 60 * 60 * 24 * 5; // 60 seconds * 60 minutes * 24 hours * 5 days
NSMutableArray *dateArray = [[dateSet allObjects] mutableCopy]; // to get the date set as an array
NSTimeInterval firstDateTimeIntervalSinceNow = [currentDate timeIntervalSinceDate: [dateArray objectAtIndex:0]];
NSTimeInterval secondDateTimeIntervalSinceNow = [currentDate timeIntervalSinceDate: [dateArray objectAtIndex:1]];
if(firstDateTimeIntervalSinceNow > secondDateTimeIntervalSinceNow)
{
[dateArray replaceObject: [dateArray objectAtIndex:0] with: currentDate];
} else {
[dateArray replaceObject: [dateArray objectAtIndex:1] with: currentDate];
}
// convert array back into set and save it...
NSSet *newSet = [[NSSet alloc] initWithArray: dateArray];
[[NSUserDefaults standardDefaults] setDateSet: newSet]; // replaces the previous date set
if(firstDateTimeIntervalSinceNow < fiveDaysTimeInterval) && (secondDateTimeIntervalSinceNow < fiveDaysTimeInterval)
{
// if the user has never seen the rating dialog before, or simply clicked on the "remind me later" button, that's
// when we should display the rating dialog
NSNumber *shouldShowRatingLater = [[NSUserDefault standardUserDefaults] objectForKey: @"RemindMeLater"];
// if RemindMeLater returns false, the rating dialog has been seen before and the user
// never wants to see it again...
if((shouldShowRatingLater == nil) || ([shouldShowRatingLater boolValue] == true))
{
return YES;
}
}
return NO;
}
- (IBAction) ratingButtonsTouched: (UIButton *) buttonTouched
{
// if the user clicks YES or NO, set "RemindMeLater" to "false" in NSUserDefaults
// if the user clicks "Remind me later", set "RemindMeLater" to "true" in NSUserDefaults
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment