Repeat UILocalNotification on Specific day

后端 未结 5 1488
滥情空心
滥情空心 2021-02-03 14:30

I need to set UILocalNotification, I just need to take hour and minute from DatePicker and need to set specific date ( like : Monday ) and repeat it on every Monday.

5条回答
  •  感情败类
    2021-02-03 14:46

    Question 1 => I can not understand what you want. (But if you want to display only name of days such like sunday, monday,....to saturday then use UIPickerView.)

    Question 2 => YES you can get correct date of specific day and you can get notification on every specific day by set localNotification.repeatInterval = NSWeekCalendarUnit;

    For achieve it, also need to use following piece of (method's) code. This method will return correct date of specific selected day, you just need to pass day as parameter of method such like,

    If selected day is sunday then pass 1
    If selected day is monday then pass 2
    .
    .
    .
    If selected day is saturday then pass 7

    Here is 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;

提交回复
热议问题