Events EventKit saveEvent在iOS 7中不工作
我在应用程序中添加了以下代码。如果[comps setYear:1]工作正常,但如果我将年份值更改为2或大于2,则代码不会显示任何错误,也不会在日历中添加任何事件。这只发生在iOS 7中。但是如果我在ios6上运行相同的代码,它就会正常工作&事件成功地添加到日历中。iOS 7中是否有添加未来事件的限制Events EventKit saveEvent在iOS 7中不工作,events,ios7,calendar,Events,Ios7,Calendar,我在应用程序中添加了以下代码。如果[comps setYear:1]工作正常,但如果我将年份值更改为2或大于2,则代码不会显示任何错误,也不会在日历中添加任何事件。这只发生在iOS 7中。但是如果我在ios6上运行相同的代码,它就会正常工作&事件成功地添加到日历中。iOS 7中是否有添加未来事件的限制 -(void)addEvent{ EKEventStore *es = [[EKEventStore alloc] init]; EKAuthorizationStatus aut
-(void)addEvent{
EKEventStore *es = [[EKEventStore alloc] init];
EKAuthorizationStatus authorizationStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
BOOL needsToRequestAccessToEventStore = (authorizationStatus == EKAuthorizationStatusNotDetermined);
if (needsToRequestAccessToEventStore) {
[es requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
[self setEventForStore:es];
} else {
}
}];
} else {
BOOL granted = (authorizationStatus == EKAuthorizationStatusAuthorized);
if (granted) {
[self setEventForStore:es];
} else {
}
}
}
-(void)setEventForStore:(EKEventStore*)store{
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = @"Event 4";
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [NSDateComponents new];
// comps.day =3650;
comps.day=5;
comps.hour=1;
comps.year=2;
NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
event.startDate = sevenDays;
NSDate *sevenDays1 = [event.startDate dateByAddingTimeInterval:60*60];;
// duration = 1 h
event.endDate = sevenDays1;
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
}
`
无论何时通过组件处理日期,请按以下方式设置:
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
[comps setDay:05];
[comps setMonth:01];
[comps setYear:2014]; //instead of adding a single digit number like so: 2
//followed by your code...
NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
//...
更新1
您提到,当您稍微调整NSDate组件中的整数值时,您的事件不会保存在日历中。一个快速查看发生了什么的方法是记录你的约会。在尝试将输出添加到日历之前,请查看输出是什么
//Right after this below line
NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
//Add the code here so we can see what `sevenDays` prints out.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSString *strDate = [dateFormatter stringFromDate:sevenDays];
NSLog(@"Seven Days date = %@", strDate);
但我的代码运行良好,如果我将年份设置为1,那么年份=2有什么问题?我尝试了您的代码,但仍然没有在日历中显示任何事件;[comps setMonth:01];[公司设置年份:2014年];然后控制台输出为7天日期=19-02-4028@Nitin好的,我明白了。它将添加到当前日期。现在的问题是,为什么在年份组件中写入2时会出现错误。也许它崩溃是因为地球将在两年后爆炸,这就是为什么日历上没有显示日期的原因P@Nitin当你把自己的号码写在原来的帖子上时,会打印出什么?您可能看到了错误的日期和时间?:)对于year=2没有错误和崩溃,但是我的代码没有添加任何year=>2的事件。