iphone - 在特定时间取消所有通知

标签 iphone objective-c ios uilocalnotification nsdatecomponents

对于我的应用程序,我不希望在周末关闭通知。 所以我的想法是在星期五的特定时间取消所有通知,我想通过安排任务来做到这一点,就像我安排通知一样(UILocalNotifications)

例如,我的闹钟代码如下:

NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setHour:8];
    [comps setMinute:25];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *fireDate = [gregorian dateFromComponents:comps];

    UILocalNotification *alarm = [[UILocalNotification alloc] init];

    alarm.fireDate = fireDate;
    alarm.repeatInterval = NSDayCalendarUnit;
    alarm.soundName = @"sound.aiff";
    alarm.alertBody = @"Message..";
    alarm.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication] scheduleLocalNotification:alarm];

有没有办法用同样的方法取消所有的通知,或者苹果不允许这样做?

最佳答案

您可以通过以下方式取消本地通知:

[[UIApplication sharedApplication] cancelLocalNotification:notification];

您需要遍历所有本地通知并在周末取消它们。循环遍历它们:

NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
[notifications enumerateObjectsUsingBlock:^(UILocalNotification *notification, NSUInteger idx, BOOL *stop){
    if (/* notification.fireDate is on weekend */) {
        [[UIApplication sharedApplication] cancelLocalNotification:notification];
    }
}];

但是,您必须确保应用程序在星期五运行才能执行删除周末通知的代码。

但是,为什么不一开始就安排周末呢?

关于iphone - 在特定时间取消所有通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13776475/

相关文章:

iphone - 如何禁用UIWebview中的放大镜?

iphone - 为什么 NSString 保留计数为 2?

ios - 在谷歌地图上绘制虚线圆圈 : iOS

ios - 如何从 iPhone 上的 Settings.app 检索信息?

ios - 根据屏幕尺寸使 UILabel 变大(iPhone 4/5/6/6+)

ios - 在 ios 中上传或下载图像的最佳方式。 FTP 与 HTTP

ios - 如何使用文本字段处理 TableView 单元格

iOS 多个选择器 View "Parse issue: Expected Expression"

ios - 环球银行金融电信协会 : Convert String from XLM to NSDate

ios - iOS:如何检查我的应用运行在哪个模拟器上?