Created
March 3, 2015 16:14
-
-
Save terrybu/280d60169dc1536fe4f8 to your computer and use it in GitHub Desktop.
UILocalNotification (M-F)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void) fireNotificationsForAllWeekDays: (NSInteger) hour minute: (NSInteger) minute { | |
[self fireNotificationForDay:Monday hour:hour minute:minute]; | |
[self fireNotificationForDay:Tuesday hour:hour minute:minute]; | |
[self fireNotificationForDay:Wednesday hour:hour minute:minute]; | |
[self fireNotificationForDay:Thursday hour:hour minute:minute]; | |
[self fireNotificationForDay:Friday hour:hour minute:minute]; | |
} | |
- (void) fireNotificationForDay: (WeekDayType) day hour: (NSInteger) hour minute: (NSInteger) minute { | |
NSDateComponents *dateComponents = [[NSDateComponents alloc]init]; | |
switch (day) { | |
//dates below are arbitrary. They just need to be some point in the past that corresponds to that weekday. | |
//when we repeat by weekly interval, it will hit us in the right times in the future | |
case Monday: { | |
[self setDateComponents:dateComponents year:2015 month:2 day:23 hour:hour minute:minute]; | |
break; | |
} | |
case Tuesday: { | |
[self setDateComponents:dateComponents year:2015 month:2 day:24 hour:hour minute:minute]; | |
break; | |
} | |
case Wednesday: { | |
[self setDateComponents:dateComponents year:2015 month:2 day:25 hour:hour minute:minute]; | |
break; | |
} | |
case Thursday: { | |
[self setDateComponents:dateComponents year:2015 month:2 day:26 hour:hour minute:minute]; | |
break; | |
} | |
case Friday: { | |
[self setDateComponents:dateComponents year:2015 month:2 day:27 hour:hour minute:minute]; | |
break; | |
} | |
default: | |
break; | |
} | |
[self fireThisNotificationWeekly:dateComponents]; | |
} | |
- (void) setDateComponents: (NSDateComponents *) dateComponents year: (NSInteger) year month:(NSInteger) month day:(NSInteger) day hour:(NSInteger) hour minute:(NSInteger) minute { | |
[dateComponents setYear:year]; | |
[dateComponents setMonth:month]; | |
[dateComponents setDay:day]; | |
[dateComponents setHour:hour]; | |
[dateComponents setMinute:minute]; | |
} | |
- (void) fireThisNotificationWeekly:(NSDateComponents *)dateComponents { | |
self.localNotif.timeZone = [[NSCalendar currentCalendar] timeZone]; | |
self.localNotif.fireDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents]; | |
self.localNotif.soundName = UILocalNotificationDefaultSoundName; | |
self.localNotif.repeatInterval = NSWeekCalendarUnit; | |
self.localNotif.alertBody = @"Did you remember to enter your timesheet today?"; | |
self.localNotif.alertAction = NSLocalizedString(@"Trackerati", nil); | |
self.localNotif.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1; | |
[[UIApplication sharedApplication] scheduleLocalNotification:self.localNotif]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment