当我创建活动时:
EKEvent *newEvent = [EKEvent eventWithEventStore:self.store];
newEvent.title = title;
newEvent.startDate = startDate;
newEvent.endDate = finishDate;
newEvent.location = location;
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:hoursOffSet * 60 * 60];
[newEvent addAlarm:alarm];
newEvent.calendar = cal;
BOOL eventSaved = [self.store saveEvent:newEvent span:EKSpanFutureEvents commit:YES error:error];
NSLog(@"identifier %@",newEvent.eventIdentifier);
if (!eventSaved) {
if(!cal)
{
*error = [NSError errorWithDomain:@"ASTEventStore" code:CALENDAR_NOT_FOUND_CODE_ERROR userInfo:NULL];
}
NSLog(@"Event didn't save in calendar %@",errorSavingInCalendar);
}
它通常是正常的,但我发现有时它会保存事件并且它不会出现任何错误,但我无法在日历应用程序中看到该事件,实际上我无法查看未创建的日历。
当我在设备中保存事件时,我总是在管理器中获取其ID - >控制台这样: :identifier /与newEvent.eventIdentifier /
相同的id你知道那个警告意味着什么吗?
////更新
好吧,我注意到当应用程序保存EKEvent并显示警告消息时,也会显示另一条消息:
:2014-08-12 10:32:28.153 | 17 | 0x178e6d500:区域监控不可用或未启用。触发器被忽略了!
我也尝试了谷歌,但我没有找到任何东西。
有什么想法吗?
谢谢:)
答案 0 :(得分:0)
嗯,我认为我找到了解决方案,问题是当我创建日历时,我以错误的方式获取源:(来自:how do I create a new EKCalendar on iOS device?)
// find local source for example
EKSource *localSource = nil;
for (EKSource *source in self.store.sources)
{
if (source.sourceType == EKSourceTypeLocal) // or another source type that supports
{
localSource = source;
break;
}
}
if (!localSource) {
NSLog(@"Local source is not available");
if (error) {
*error = [NSError errorWithDomain:@"ASTEventStore" code:CALENDAR_ACCOUNT_NOT_TYPE_LOCAL userInfo:NULL];
}
}
else{
cal = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.store];
cal.title = @"Calendar";
cal.source = self.store.defaultCalendarForNewEvents.source;
cal.CGColor = [UIColor blueColor].CGColor;
NSError *calendarError;
BOOL calendarSaved = [self.store saveCalendar:cal commit:YES error:&calendarError];
if (!calendarSaved) {
NSLog(@"calendar didn't saved %@",calendarError);
}
NSLog(@"cal id = %@", cal.calendarIdentifier);
//We save the calendar ID
calendarId = cal.calendarIdentifier;
[defaults setObject:calendarId forKey:@"calendarId"];
BOOL saved = [defaults synchronize];
if (!saved) {
NSLog(@"CALENDAR_ID didn't saved");
}
}
现在我来自defaultCalendarForNewEvents:
cal = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.store];
cal.title = @"Calendar";
cal.source = self.store.defaultCalendarForNewEvents.source;
cal.CGColor = [UIColor blueColor].CGColor;
NSError *calendarError;
BOOL calendarSaved = [self.store saveCalendar:cal commit:YES error:&calendarError];
if (!calendarSaved) {
NSLog(@"calendar didn't saved %@",calendarError);
}
NSLog(@"cal id = %@", cal.calendarIdentifier);
if (!saved) {
NSLog(@"CALENDAR_ID didn't saved");
}
我测试过,它在iOS7中运行良好。
我希望这对某人有所帮助:)。