【问题标题】:UILocalNotification set repeatCalendarUILocalNotification 设置重复日历
【发布时间】:2014-02-13 15:13:44
【问题描述】:

我是 NSCalanderNSdatesNSDateComponents 的新手

基本上我有一个本地通知,我想根据用户的选择重复触发日期,比如只在周日和周一。

我们应该为UILocalNotification 使用repeatCalendar 属性,但我不知道如何设置它。

那么任何人都可以用简单的代码行帮助我吗?

谢谢

【问题讨论】:

    标签: ios nsdate uilocalnotification nscalendar nsdatecomponent


    【解决方案1】:

    有一个代码 sn-p 将 UILocalNotification 设置为在每个星期日 20:00 触发。

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *now = [NSDate date];
    NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit|  NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
    [componentsForFireDate setWeekday: 1] ; //for fixing Sunday
    [componentsForFireDate setHour: 20] ; //for fixing 8PM hour
    [componentsForFireDate setMinute:0] ;
    [componentsForFireDate setSecond:0] ;
    
    NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
    UILocalNotification *notification = [[UILocalNotification alloc]  init] ;
    notification.fireDate = fireDateOfNotification ;
    notification.timeZone = [NSTimeZone localTimeZone] ;
    notification.alertBody = [NSString stringWithFormat: @"New updates!"] ;
    notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"New updates added for that week!"] forKey:@"new"];
    notification.repeatInterval= NSWeekCalendarUnit ;
    notification.soundName=UILocalNotificationDefaultSoundName;
    
    NSLog(@"notification: %@",notification);
    
    [[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
    

    享受

    【讨论】:

    • 一件事,你的答案将是最好的;)如何添加多天?说周日和周一?
    • 然后可以设置两个通知。每个星期天一个,每个星期一一个。
    • 这是我要解决的问题,确定 Apple 没有为同一事件制作两次通知的 repeatCalendar 属性,假设我要进行 10 次提醒并在 5 天后提醒我本周,那么我应该创建 50 个通知?!不要做场景我认为有一种方法可以合并日历中的日子,比如遮罩或其他东西!
    • 我搜索了更多,我认为你的答案是正确的,我要为每一天设置一个提醒。 stackoverflow.com/questions/5938528/…
    猜你喜欢
    • 2012-09-27
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多