Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeat UILocalNotification on Specific day

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.

I have two question about it :

First one ; is it possible to show only "day names" like "Sunday" on the date section of date picker?

Second question is ; what is the correct codes if i want to set specific date for local notification ?

Thank you 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];
like image 960
Rick Avatar asked Jan 31 '14 14:01

Rick


1 Answers

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;

like image 86
iPatel Avatar answered Nov 12 '22 20:11

iPatel