Repeat UILocalNotification on a specific day

I need to set UILocalNotification, I just need to take an hour and a minute from DatePicker and set a specific date (for example: Monday) and repeat it every Monday.

I have two questions:

First; is it possible to show only "day names", for example, "sunday" in the date picker section?

Second question: What are the correct codes if I want to set a specific date for local notification?

Thanks for the answers, and here is my code below;

-(IBAction) alarmButton:(id)sender { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; dateFormatter.timeZone = [NSTimeZone defaultTimeZone]; dateFormatter.timeStyle = NSDateFormatterShortStyle; dateFormatter.dateStyle = NSDateFormatterShortStyle; NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePickerSet1.date]; NSLog ( @"Alarm Set :%@" , dateTimeString); [self scheduleLocalNotificationWithDate1: dateTimePickerSet1.date]; } -(void)scheduleLocalNotificationWithDate1:(NSDate *)fireDate { UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.fireDate = fireDate; notification.alertBody = @"Alarm Set !"; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
+6
source share
5 answers

Question 1 => I do not understand what you want. (But if you only want to display the name of the days, such as Sunday, Monday, .... until Saturday , use UIPickerView. )

Question 2 => YES, you can get the correct date for a particular day, and you can get a notification on every specific day by setting localNotification.repeatInterval = NSWeekCalendarUnit;

To achieve this, it is also necessary to use the following code fragment (method). This method will return the correct date for the specific selected day, you just need to pass the day as a parameter to a method, such as

If the selected day is sunday , go 1
If the selected day is Monday , go through 2
.
.
.
If the selected day is Saturday , go to 7

Here is the method code:

 -(NSDate *) getDateOfSpecificDay:(NSInteger ) day /// here day will be 1 or 2.. or 7 { NSInteger desiredWeekday = day NSRange weekDateRange = [[NSCalendar currentCalendar] maximumRangeOfUnit:NSWeekdayCalendarUnit]; NSInteger daysInWeek = weekDateRange.length - weekDateRange.location + 1; NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]]; NSInteger currentWeekday = dateComponents.weekday; NSInteger differenceDays = (desiredWeekday - currentWeekday + daysInWeek) % daysInWeek; NSDateComponents *daysComponents = [[NSDateComponents alloc] init]; daysComponents.day = differenceDays; NSDate *resultDate = [[NSCalendar currentCalendar] dateByAddingComponents:daysComponents toDate:[NSDate date] options:0]; return resultDate; } 

And don't forget to set localNotification.repeatInterval = NSWeekCalendarUnit;

+8
source

Here's the @iPatel solution, which also affects the time component passed through the datepicker. (I also updated his solution to use the latest calendar units that are not outdated).

 - (NSDate *) getFireDateForDayOfWeek:(NSInteger)desiredWeekday withTime:(NSDate *)time // 1:Sunday - 7:Saturday { NSRange weekDateRange = [[NSCalendar currentCalendar] maximumRangeOfUnit:NSCalendarUnitWeekday]; NSInteger daysInWeek = weekDateRange.length - weekDateRange.location + 1; NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:[NSDate date]]; NSInteger currentWeekday = dateComponents.weekday; NSInteger differenceDays = (desiredWeekday - currentWeekday + daysInWeek) % daysInWeek; NSDateComponents *daysComponents = [[NSDateComponents alloc] init]; daysComponents.day = differenceDays; NSDate *fireDate = [[NSCalendar currentCalendar] dateByAddingComponents:daysComponents toDate:[NSDate date] options:0]; NSDateComponents *timeComponents = [[NSCalendar currentCalendar] components: NSCalendarUnitHour | NSCalendarUnitMinute fromDate:time]; NSDateComponents *fireDateComponents = [[NSCalendar currentCalendar] components: NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:fireDate]; fireDateComponents.hour = timeComponents.hour; fireDateComponents.minute = timeComponents.minute; fireDateComponents.second = 0; NSDate *resultDate = [[NSCalendar currentCalendar] dateFromComponents:fireDateComponents]; // The day could be today but time is in past. If so, move ahead to next week if(resultDate.timeIntervalSinceNow < 0) { resultDate = [resultDate dateByAddingTimeInterval:60 * 60 * 24 * 7]; } return resultDate; } 
0
source

This solution also takes time, but it’s a bit simplified than

 -(NSDate *) dateOfSpecificDay:(NSInteger ) day withFireTime:(NSDate *)timeDate/// here day will be 1 or 2.. or 7 and timeDate the current day with the desired time { NSInteger desiredWeekday = day; NSRange weekDateRange = [[NSCalendar currentCalendar] maximumRangeOfUnit:NSCalendarUnitWeekday]; NSInteger daysInWeek = weekDateRange.length - weekDateRange.location + 1; NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:[NSDate date]]; NSInteger currentWeekday = dateComponents.weekday; NSInteger differenceDays = (desiredWeekday - currentWeekday + daysInWeek) % daysInWeek; if (differenceDays == 0) { //Check if should register for same day or for next week if ([timeDate compare:[NSDate date]] == NSOrderedAscending) { //Should schedule for next week differenceDays = daysInWeek; } } NSDateComponents *daysComponents = [[NSDateComponents alloc] init]; daysComponents.day = differenceDays; NSDate *resultDate = [[NSCalendar currentCalendar] dateByAddingComponents:daysComponents toDate:timeDate options:0]; return resultDate; } 
0
source

Here is the swift version:

 func calculateTheNExtFriday(inputTime: NSDate) -> NSDate { let desiredWeekday = 6 // Friday let weekDateRange = NSCalendar.currentCalendar().maximumRangeOfUnit(NSCalendarUnit.Weekday) let daysInWeek = weekDateRange.length - weekDateRange.location + 1; let currentWeekday = NSCalendar.currentCalendar().components(NSCalendarUnit.Weekday, fromDate: inputTime).weekday let differenceDays = (desiredWeekday - currentWeekday + daysInWeek) % daysInWeek let dateComponents = NSDateComponents() dateComponents.day = differenceDays let calculatedFridayDate = NSCalendar.currentCalendar().dateByAddingComponents(dateComponents, toDate: inputTime, options: NSCalendarOptions.init(rawValue: 0)) return calculatedFridayDate! } 

In this example, I use the alwaysWeekday function inside the function, but you can also use it as a parameter.

NB: Keep in mind that day 1 corresponds to Sundays, 2 β†’ Mondays, and so

0
source

Swift 3:

 func calculateSpecificDay(_ desiredWeekday: NSInteger, fromDate: Date = Date()) -> Date { let weekDateRange = NSCalendar.current.maximumRange(of: .weekday)! let daysInWeek = weekDateRange.lowerBound.distance(to: weekDateRange.upperBound) - weekDateRange.lowerBound + 1 let currentWeekday = NSCalendar.current.dateComponents([.weekday], from: fromDate).weekday let differenceDays = (desiredWeekday - currentWeekday! + daysInWeek) % daysInWeek var dateComponents = DateComponents() dateComponents.day = differenceDays let calculatedFridayDate = NSCalendar.current.date(byAdding: dateComponents, to: fromDate) return calculatedFridayDate! } 
 If selected day is sunday then pass 1 If selected day is monday then pass 2 . . . If selected day is saturday then pass 7 
0
source

All Articles