gpt4 book ai didi

ios - Eventkit 并不总是删除事件

转载 作者:行者123 更新时间:2023-11-29 02:55:24 33 4
gpt4 key购买 nike

我正在开发一款应用程序,可以在线获取学生的学校类(class)并将其保存到 iPhone 的日历中。

每次类(class)更新时,我想从本周的日历中删除所有事件,然后将整周的更新类(class)放入。

我在添加事件时没有遇到任何问题,但有时事件不会被删除?

dispatch_queue_t queue = dispatch_queue_create("com.xxxxr.xxxxxx.calendar", NULL);
dispatch_async(queue, ^{
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {

if (granted){

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
EKCalendar *calendarIdentifier;

if ([defaults objectForKey:@"Calendar"] == nil || ![eventStore calendarWithIdentifier:[defaults objectForKey:@"Calendar"]]){
// Create Calendar if Needed

EKSource *theSource = nil;
for (EKSource *source in eventStore.sources) {
if (source.sourceType == EKSourceTypeCalDAV && [source.title isEqualToString:@"iCloud"]) {
theSource = source;
NSLog(@"iCloud Store Source");
break;
} else {
for (EKSource *source in eventStore.sources) {
if (source.sourceType == EKSourceTypeLocal) {
theSource = source;
NSLog(@"iPhone Local Store Source");
break;
}
}
}
}

EKCalendar *cal;
cal = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore];
cal.title = @"xxxxxxx";
cal.source = theSource;
[eventStore saveCalendar:cal commit:YES error:nil];
NSString *calendar_id = cal.calendarIdentifier;
[defaults setObject:calendar_id forKey:@"Calendar"];
calendarIdentifier = cal;

} else {
calendarIdentifier = [eventStore calendarWithIdentifier:[defaults objectForKey:@"Calendar"]];
NSLog(@"Calendar Existed");
}


/* NOW TO WHERE THE PROBLEM LIES! */
/* FIRST DELETE ALL EVENTS OF THIS WEEK */

NSArray *arrayOfIdentitiesToDelete = [defaults objectForKey:@"eventIdentities"];
for(NSString *identifierOfEventToDelete in arrayOfIdentitiesToDelete){
EKEvent *eventToRemove = [eventStore eventWithIdentifier:identifierOfEventToDelete];
NSError *error;
[eventStore removeEvent:eventToRemove span:EKSpanThisEvent error:&error];
}

/* Then create new events from my 'arrayOfLessons' array */

NSMutableArray *arrayOfEventIdentities = [[NSMutableArray alloc] init];

for(int dayAddingToCalendar = 0; dayAddingToCalendar < 5; dayAddingToCalendar++){
for(NSArray *arrayOfDayAddingToCalendar in [[arrayOfLessons objectAtIndex:dayAddingToCalendar] objectAtIndex:3]){

EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.calendar = calendarIdentifier;
event.title = [arrayOfDayAddingToCalendar objectAtIndex:4];
event.location = [arrayOfDayAddingToCalendar objectAtIndex:1];
event.notes = [arrayOfDayAddingToCalendar objectAtIndex:0];
event.startDate = [arrayOfDayAddingToCalendar objectAtIndex:12];
event.endDate = [arrayOfDayAddingToCalendar objectAtIndex:13];
event.allDay = NO;

[eventStore saveEvent:event span:EKSpanThisEvent error:nil];
[arrayOfEventIdentities addObject:event.eventIdentifier];
}
}

[defaults setObject:arrayOfEventIdentities forKey:@"eventIdentities"];

} else {
NSLog(@"Not Granted");
}
}];
});

最佳答案

你是说:

NSError *error;
[eventStore removeEvent:eventToRemove span:EKSpanThisEvent error:&error];

所以你放弃了错误检查。不。查看错误!这就是它的用途。像这样:

NSError *error;
BOOL ok = [eventStore removeEvent:eventToRemove span:EKSpanThisEvent error:&error];
if (!ok)
NSLog(@"%@", error);

关于ios - Eventkit 并不总是删除事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23963457/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com