An app I am working on will have a daily message in it for every weekday of a year. I have the PDFs for each weekday in folders named Mondays, Tuesdays, Wednesdays, Thursdays, Fridays. Each PDF is named 0.pdf, 1.pdf, etc. so that I can increment a count. The below is my code for displaying, but the issue I am having is that it increments the count every time the view is loaded. What would be a good way to not advance to the next PDF until it was a different date from the previous one?
- (void)viewDidLoad {
NSDateFormatter* day = [[NSDateFormatter alloc] init];
[day setDateFormat: @"EEEE"];
NSLog(@"the day is: %@", [day stringFromDate:[NSDate date]]);
NSString *dayString = [day stringFromDate:[NSDate date]];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSInteger whichWeekMonday = [defaults integerForKey:@"DevoWeekMonday"];
NSInteger whichWeekTuesday = [defaults integerForKey:@"DevoWeekTuesday"];
NSInteger whichWeekWednesday = [defaults integerForKey:@"DevoWeekWednesday"];
NSInteger whichWeekThursday = [defaults integerForKey:@"DevoWeekThursday"];
NSInteger whichWeekFriday = [defaults integerForKey:@"DevoWeekFriday"];
NSLog(@"Integer Thursday: %ld", (long)whichWeekThursday);
NSString *whichWeekMondayString = [[NSNumber numberWithInteger:whichWeekMonday] stringValue];
NSString *whichWeekTuesdayString = [[NSNumber numberWithInteger:whichWeekTuesday] stringValue];
NSString *whichWeekWednesdayString = [[NSNumber numberWithInteger:whichWeekWednesday] stringValue];
NSString *whichWeekThursdayString = [[NSNumber numberWithInteger:whichWeekThursday] stringValue];
NSLog(@"Thursday string: %@", whichWeekThursdayString);
NSString *whichWeekFridayString = [[NSNumber numberWithInteger:whichWeekFriday] stringValue];
if ([dayString isEqualToString:@"Monday"]) {
NSString *Documents = [[NSBundle mainBundle] pathForResource:whichWeekMondayString ofType:@"pdf" inDirectory:@"Mondays"];
NSURL *url = [NSURL fileURLWithPath:Documents];
NSLog(@"%@",url);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[savedweb loadRequest:request];
NSInteger updatedNumberMonday = whichWeekMonday + 1;
[defaults setInteger:updatedNumberMonday forKey:@"DevoWeekMonday"];
[defaults synchronize];
}
if ([dayString isEqualToString:@"Tuesday"]) {
NSString *Documents = [[NSBundle mainBundle] pathForResource:whichWeekTuesdayString ofType:@"pdf" inDirectory:@"Tuesdays"];
NSURL *url = [NSURL fileURLWithPath:Documents];
NSLog(@"%@",url);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[savedweb loadRequest:request];
NSInteger updatedNumberTuesday = whichWeekTuesday +1;
[defaults setInteger:updatedNumberTuesday forKey:@"DevoWeekTuesday"];
[defaults synchronize];
}
if ([dayString isEqualToString:@"Wednesday"]) {
NSString *Documents = [[NSBundle mainBundle] pathForResource:whichWeekWednesdayString ofType:@"pdf" inDirectory:@"Wednesdays"];
NSURL *url = [NSURL fileURLWithPath:Documents];
NSLog(@"%@",url);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[savedweb loadRequest:request];
NSInteger updatedNumberWednesday = whichWeekWednesday +1;
[defaults setInteger:updatedNumberWednesday forKey:@"DevoWeekWednesday"];
[defaults synchronize];
}
if ([dayString isEqualToString:@"Thursday"]) {
// perform some action
NSString *Documents = [[NSBundle mainBundle] pathForResource:whichWeekThursdayString ofType:@"pdf" inDirectory:@"Thursdays"];
NSLog(@"File: %@", Documents);
NSURL *url = [NSURL fileURLWithPath:Documents];
NSLog(@"%@",url);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[savedweb loadRequest:request];
NSInteger updatedNumberThursday = whichWeekThursday +1;
[defaults setInteger:updatedNumberThursday forKey:@"DevoWeekThursday"];
[defaults synchronize];
}
if ([dayString isEqualToString:@"Friday"]) {
NSString *Documents = [[NSBundle mainBundle] pathForResource:whichWeekFridayString ofType:@"pdf" inDirectory:@"Fridays"];
NSURL *url = [NSURL fileURLWithPath:Documents];
NSLog(@"%@",url);
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[savedweb loadRequest:request];
NSInteger updatedNumberFriday = whichWeekFriday +1;
[defaults setInteger:updatedNumberFriday forKey:@"DevoWeekFriday"];
[defaults synchronize];
}
if ([dayString isEqualToString:@"Saturday"]) {
}
if ([dayString isEqualToString:@"Sunday"]) {
}
// NSString *Documents = [[NSBundle mainBundle] pathForResource:selectedCountry ofType:@"pdf" inDirectory:@"thepdfpowerpoints"];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
Based on your current question of getting the the count consistently without incrementing every time the view is loading:
I believe leaving count number to be dynamic is better. So you'd use your day string to determine which folder and then dynamically figure out which numbered pdf based on the week of the year so far using something like this:
NSDateComponents weekofyearComponents = [[NSCalendar(calendarIdentifier: NSCalendarIdentifierISO8601)] components:(NSCalendarUnitWeekOfYear ) fromDate: [NSDate date]];
NSInteger whichWeekOfYear = weekofyearComponents.weekOfYear
I can see a couple ways you could do it.
One way would be like you suggested, giving the number of the day of the year to the PDF for that day. Here is a link for getting the day of the year in iOS swift.
Another way would be to get the week of the year number(1-52) and then get the day of the week number so you could organize the pdf’s in directories by week of the year and then within it be the messages for each day of the week number.
My last comment would be that PDF’s sound a little heavy or clunky for a message of the day in an iOS app. A text file may suffice or some lightweight database or some cache that’s updated with a webrequest to refresh and store the current and next several messages of the day. I am definitely lacking context so I don’t dismiss the fact that PDFs may be exactly what you need.