开发者

Pass value of kCFCalendarUnitWeekday

I want to set an alarm on a particular day. I don't understand how to set the value of kCFCalendarUnitWeekday. Here is my code:

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

if (isSun) {
    [components setWeekday:1];

    [localNotif setRepeatInterval:(NSInteger)components];
}

if (isMon) {
    [components setWeekday:2];
    [localNotif  set开发者_JS百科RepeatInterval:(NSInteger)components];  
}

Thank you.


Ok here is some code to get you in the right direction:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSWeekdayCalendarUnit|NSYearCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:[NSDate date]];

    // this will set the weekday to sunday
[components setWeekday:1];
    // this is a new date on sunday
NSDate * newDate = [gregorian dateFromComponents:components];

You still have to find out whether the newDate is in the past, so it won't fire.


You actually have your code ready:

// ... init code ...

// *** this the important date ***
localNotif.fireDate = dateOnASunday;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatCalendar = NSWeekCalendarUnit;

... // add body etc. ...

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];

Create a method around this snippet and pass the date with a parameter so you can reuse it. You only have to put in a date which should be the first fire date and it'll repeat every week. You just have to pass in a date on a Sunday or Wednesday.

You set a repeat interval like this:

// repeats notification on a weekly basis
localNotif.repeatCalendar = NSWeekCalendarUnit;

The repeatCalender property is of type NSCalendarUnit which is an enum.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜