XCode: Why is my event not being added to the calendar?

非 Y 不嫁゛ 提交于 2019-12-01 17:16:53

问题


I have added the following code to my app:

NSDate * selected = [DatePicker date];
NSString * date = [selected description];

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit 
                                                              fromDate:DatePicker.date];

CalLabel1.text = [NSString stringWithFormat:@"%d", [components day]];

EKEventStore *store = [[EKEventStore alloc] init];

EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = @"Test Event";
//required
event.startDate = [DatePicker date];
event.endDate = [DatePicker date];
event.calendar = store.defaultCalendarForNewEvents;
//end

NSError *err;
[store saveEvent:event span:EKSpanThisEvent error:&err];

Why is this not adding anything to the iOS calendar?

Thanks.


回答1:


Your event needs a valid time span to save it. Try this

event.startDate = selected;
event.endDate = [selected dateByAddingTimeInterval:30*60]; //30 minutes for example


来源:https://stackoverflow.com/questions/7718006/xcode-why-is-my-event-not-being-added-to-the-calendar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!