开发者

UILocalNotifications repeatInterval using NSWeekdayCalendarUnit

开发者 https://www.devze.com 2023-02-14 12:30 出处:网络
OK, so I have seen several posts that say you cannot set a UILocalNotification to repeat any more or less than a few given options (every minute/hour/day/week/month etc.).

OK, so I have seen several posts that say you cannot set a UILocalNotification to repeat any more or less than a few given options (every minute/hour/day/week/month etc.).

However, none of those posts addressed what setting the repeatInterval property of UILocalNotification to NSWeekdayCalendarUnit would do.

I'm very new to all this NSDate and NSCalendar stuff, so I am sure I am missing something, but I have read over the documentation and, it sounds like you CAN use NSWeekdayCalendarUnit to make a NSLocalNotification repeat say, every Monday, Tuesday and Thursday if NSWeekdayCalendarUnit is set to 2,3,5.

NSWeekdayCalendarUnit Specifies the weekday unit. The corresponding value is an int. Equal to kCFCalendarUnitWeekday. The weekday units are the numbers 1 through N (w开发者_如何学Gohere for the Gregorian calendar N=7 and 1 is Sunday).

Is that not correct?

Thanks in advance.


Yes you can. I do it like this. The user can choose a scheme with a picker. And then the choice goes to the following method:

- (void)setOneLocalAlarmNotification: (NSDate *)firstFireDate withRepeat: (NSInteger)repeat {

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
    return;
localNotif.fireDate = firstFireDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatCalendar = [NSCalendar currentCalendar];

switch(repeat) {
    case 0: 
        break ;
    case 1: 
        localNotif.repeatInterval = kCFCalendarUnitDay;
        break;
    case 2: 
        localNotif.repeatInterval = kCFCalendarUnitWeekday;
        break;
    default: 
        break;
}

// Notification details
localNotif.alertBody = @"Message?";
// Set the action button
localNotif.alertAction = @"Yes";

localNotif.soundName = @"glas.caf"; //UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;

// Specify custom data for the notification
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"Alarm" forKey:@"type"];
   localNotif.userInfo = infoDict;

// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release]
}
0

精彩评论

暂无评论...
验证码 换一张
取 消