0

I want display local notification on my app in a specific day, for example, I want the notification is displayed all the 1 of the month.

I have this code, how can I customize it ?

//LOcal Notification

    NSDate *today = [NSDate new];
    NSCalendar *gregorian =
    [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *offsetComponents = [NSDateComponents new];
    [offsetComponents setDay:???];
    [offsetComponents setHour:10];
    [offsetComponents setMinute:0];

    NSDate *thedate = [gregorian dateByAddingComponents:offsetComponents
                                                  toDate:today options:0];
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = thedate;
    localNotification.alertBody = @"Hey, il serait temps de faire un point non ? \ue104";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    //END

There is too a depreciated error with ios8 on the gregorian calendar, how can I resolve it ?

ewan
  • 276
  • 2
  • 17
  • 1
    Depreciated is warning not error, you can still use it. Press `cmd + click` on `NSGregorianCalendar ` will guide you what to use instead of Deprecated one. – iphonic Apr 30 '15 at 19:36
  • Xcode says : FOUNDATION_EXPORT NSString * const NSGregorianCalendar NS_CALENDAR_DEPRECATED(10_4, 10_10, 2_0, 8_0, "Use NSCalendarIdentifierGregorian instead"); , ok but how ? and where ? – ewan Apr 30 '15 at 19:41
  • Both questions have been asked before: http://stackoverflow.com/questions/25626823/how-to-support-nscalendar-with-both-ios-7-and-8 http://stackoverflow.com/questions/6966365/uilocalnotification-repeat-interval-for-custom-alarm-sun-mon-tue-wed-thu-f – Tapani Apr 30 '15 at 19:42

1 Answers1

0
//assuming you want to fire the notifiaction on 1st of every month.
 NSInteger desiredWeekday = 1;// it specifies on which day  of month 
  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];


UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = resultDate; 
    localNotification.alertBody = @"Hey, il serait temps de faire un point non ? \ue104";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];

    localNotification.repeatInterval = NSMonthCalendarUnit;// repeat notifiaction after a month
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Vizllx
  • 9,135
  • 1
  • 41
  • 79