UILocalNotification is supposed to repeat every weekday, but fires on weekends as well

后端 未结 4 974
无人共我
无人共我 2020-12-28 09:36

I have a UILocalNotification that is supposed to fire once a day, Monday through Friday, but not on the weekend. I thought that setting the repeatInterval property of the no

4条回答
  •  一生所求
    2020-12-28 10:23

    I have also searched about it. Below code work good for me. Pass the week day value 1 to 7 Sunday to Saturday and notification body with action which you want to fire and specify your date then notification will come on that specific day. For weekends call this function twice with value 7 and 1 for Saturday and Sunday.

    Hope this help you.

    - (void) weekEndNotificationOnWeekday: (int)weekday :(UILocalNotification *)notification : (NSDate*) alramDate
    {
        NSCalendar *calendar = [NSCalendar currentCalendar];
    
        NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: alramDate];
        [componentsForFireDate setWeekday: weekday] ; //for fixing Sunday
        //    [componentsForFireDate setHour: 20] ; //for fixing 8PM hour
        //    [componentsForFireDate setMinute:0] ;
        //    [componentsForFireDate setSecond:0] ;
        notification.repeatInterval = NSWeekCalendarUnit;
        notification.fireDate=[calendar dateFromComponents:componentsForFireDate];
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    
    }
    

提交回复
热议问题