我试图从应用程序中保存iPhone日历上的事件,我正在使用此代码:
- (void)addReminderWithTitle:(NSString *)title date:(NSString *)date {
NSString *dateString = date;
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
[dateFromString dateByAddingTimeInterval:-60*30];
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = @"Event Title";
event.startDate = [NSDate date]; //today
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
event.calendar = [eventStore defaultCalendarForNewEvents];
NSError *err = nil;
[eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
它没有保存任何错误,我得到了:错误获取新事件的默认日历:错误域= ekcaderrordomain code = 1013"(null("
有帮助吗?
我使用您的代码,我在info.plist NSCalendarsUsageDescription
NSRemindersUsageDescription
中添加了两个属性然后我nslog(@" eventStore =:%@",eventStore(;结果是eventStore=:<EKEventStore: 0x17418ce60>
我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
[dateFromString dateByAddingTimeInterval:-60*30];
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = @"Event Title";
event.startDate = [NSDate date]; //today
event.endDate = [event.startDate dateByAddingTimeInterval:60*60]; //set 1 hour meeting
event.calendar = [eventStore defaultCalendarForNewEvents];
NSError *err = nil;
[eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
}}];
NSLog(@"eventStore=:%@", eventStore);
}
这是您要做的事情的迅速代码。您要求访问提醒而不是事件,这就是为什么您会遇到此错误的原因。
let store = EKEventStore()
// Request access to calendar first
store.requestAccess(to: .event, completion: { (granted, error) in
if granted {
print("calendar allowed")
// create the event object
let event = EKEvent(eventStore: store)
event.title = self.activityPara.actName
event.startDate = self.activityPara.actDate
event.endDate = self.activityPara.actDate.addingTimeInterval(3600) // 1 hr time
event.location = self.activityPara.actLocatnStr
event.calendar = store.defaultCalendarForNewEvents
let controller = EKEventEditViewController()
controller.event = event
controller.eventStore = store
controller.editViewDelegate = self
self.present(controller, animated: true)
}
else
{
print("calendar not allowed")
}
})
还确保在您的信息中包括隐私 - 日历使用情况描述。