簡體   English   中英

如何檢測事件存儲更改以及更改的內容? 來自EKEventStoreChangedNotification

[英]How to detect event store changes and what is change? From EKEventStoreChangedNotification

如何知道是否添加或刪除了EKEvent? 這樣我就可以安排活動通知。 否則對於已刪除的事件,它也會安排通知。 任何幫助?

 -(void)storeChanged:(NSNotification*)notification{

    EKEventStore *ekEventStore = notification.object;

    NSDate *now = [NSDate date];
    NSDateComponents *offsetComponents = [NSDateComponents new];
    [offsetComponents setDay:0];
    [offsetComponents setMonth:4];
    [offsetComponents setYear:0];
    NSDate *endDate = [[NSCalendar currentCalendar] dateByAddingComponents:offsetComponents toDate:now options:0];

    NSArray *ekEventStoreChangedObjectIDArray = [notification.userInfo objectForKey:@"EKEventStoreChangedObjectIDsUserInfoKey"];
    NSPredicate *predicate = [ekEventStore    predicateForEventsWithStartDate:now
                                                                      endDate:endDate
                                                                    calendars:nil];
    // Loop through all events in range
    [ekEventStore enumerateEventsMatchingPredicate:predicate usingBlock:^(EKEvent *ekEvent, BOOL *stop) {
        // Check this event against each ekObjectID in notification
        [ekEventStoreChangedObjectIDArray enumerateObjectsUsingBlock:^(NSString *ekEventStoreChangedObjectID, NSUInteger idx, BOOL *stop) {
            NSObject *ekObjectID = [(NSManagedObject *)ekEvent objectID];
            if ([ekEventStoreChangedObjectID isEqual:ekObjectID]) {
                // Log the event we found and stop (each event should only exist once in store)
                NSLog(@"calendarChanged(): Event Changed: title:%@", ekEvent.title);

                NSLog(@"%@",ekEvent);

                NSInteger total = [[[UIApplication sharedApplication] scheduledLocalNotifications]count];
                if (total == 64) {
                    [self cancelLastNotification];
                }

                NSArray *newArray = [[NSArray alloc]initWithObjects:ekEvent, nil];
                [self scheduleNotificationForCalendarEvents:NO andEvents:newArray];

                *stop = YES;
            }
        }];
    }];

}

如果使用refresh屬性,它將在刪除時返回false,如果仍然可以使用,則返回true。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM