作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一款应用程序,可以在线获取学生的学校类(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/
我是一名优秀的程序员,十分优秀!