Search code examples
objective-cnsdateuilocalnotificationalarmnscalendar

Repeat alarm on weekly basis


I am implementing the alarm application. I am stuck with the logic. How to repeat the alarm on weekly basic, mean if user selected the sunday than alarm will be rang on every sunday... Here is my code.

-(void) repeataftersevendays: (NSString *) day
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
    [components setTimeZone:[NSTimeZone localTimeZone]];
    [calendar setTimeZone: [NSTimeZone localTimeZone]];
    sunday = [calendar dateByAddingUnit:NSCalendarUnitDay
                                       value:4
                                      toDate:[NSDate date]
                                          options:0];;
    NSLog(@"value is %@",sunday);

}

and here is my notification fire code.

-(void) scheduleLocalNotificationWithDate:(NSDate *)fireDate
{

 UILocalNotification *localNotif = [[UILocalNotification alloc] init];

    localNotif.fireDate = fireDate;
    localNotif.timeZone = [NSTimeZone localTimeZone];
    localNotif.alertBody = @"Time to wake Up";
    localNotif.alertAction = @"Show me";
    localNotif.soundName = @"Tick-tock-sound.mp3";
    localNotif.applicationIconBadgeNumber = 1;
    localNotif.repeatInterval = kCFCalendarUnitDay;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];


}

Solution

  • you can use repeat interval parameter of NSLocalNotification. set it to NSWeekCalendarUnit

    localNotification.repeatInterval = NSWeekCalendarUnit;