I am trying to display a different website on each day of the week. I created a NSString that contains just the current day of the week by using NSDateFormatter. Then, I created additional strings for each day of the week. I am comparing the two in an "IF" Statement...so if the strings (days) are equal, it will perform the function in the if statement. if not, it checks the next statement. Right now it will work for the first statement on Monday, but when I change the date on my iPhone to simulate other days of the week it won't work. My code is below!
NSDateFormatter *dayofweekformatter = [[NSDateFormatter alloc] init];
[dayofweekformatter setDateFormat:@"cccc"];
NSString *DayOfWeek = [dayofweekformatter stringFromDate:[NSDate date]];
NSString *Monday = @"Monday";
NSString *Tuesday = @"Tuesday";
NSString *Wednesday = @"Wednesday";
NSString *Thursday = @"Thursday";
NSString *Friday = @"Friday";
NSString *Saturday = @"Saturday";
NSString *Sunday = @"Sunday";
if ([DayOfWeek isEqualToString:Monday])
{ // Webview code
NSString *urlAddress = @"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webview loadRequest:requestObj];
}
else if (dateToday == Tuesday)
{ // Webview code
NSString *urlAddress = @"http://www.cnn.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webview loadRequest:requestObj];
A better solution would be the following, using the index of the weekday to determine your url:
To refresh your checks as you asked on a comment, you could do this:
In you application delegate file add this line to the applicationDidBecomeActive: method
Over in your class you are doing your date checking, in the init method add this line to listen out for any refresh notifications sent when the app comes out of the background:
Finally move over your date check code to this method which is called whenever the notification is received: