ios – 如何修复EKErrorDomain Code = 1“没有设置日历

前端之家收集整理的这篇文章主要介绍了ios – 如何修复EKErrorDomain Code = 1“没有设置日历前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想创建一个日历条目到iPhone日历,我尝试了以下代码
EKEventStore *eventStore = [[EKEventStore alloc] init];
        EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
        event.title     = self.selectedPost.postTitle;
        event.notes     = self.selectedPost.postContent;
        event.startDate =  self.selectedPost.startDate;
        event.endDate   =  self.selectedPost.endDate;

        EKCalendar *targetCalendar = nil;
        targetCalendar = [eventStore defaultCalendarForNewEvents];
        NSLog(@"%@",targetCalendar);
        [event setCalendar:targetCalendar];
        NSError *err;
        [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
        UIAlertView *alert = nil;
        NSLog(@"err %@",err);
        if (err) {
            alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[err localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        }
        else{
            alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Added to calender" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        }

        [alert show];

但结果是

2013-01-15 22:31:34.682 Project[40863:907] defaultCalendarForNewEvents Failed: Error Domain=EKCADErrorDomain Code=1013 "The operation couldn’t be completed. (EKCADErrorDomain error 1013.)"
2013-01-15 22:31:34.683 Project[40863:907] (null)
2013-01-15 22:31:34.690 Project[40863:907] err Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x1d535ba0 {NSLocalizedDescription=No calendar has been set.}

我知道这是因为

[eventStore defaultCalendarForNewEvents];

返回null.
我试过了
[eventStore calendarWithIdentifier:event.calendarItemIdentifier];
和一些其他代码但相同的结果如何解决这个问题
任何的想法

解决方法

如果这是在iOS 6.0或更高版本,您必须首先请求访问用户的日历,然后EventKit将使用方法 -[EKEventStore requestAccessToEntityType:completion:]将它们交给您

查看Calendar and Reminders Programming Guide中给出的示例

原文链接:https://www.f2er.com/iOS/327851.html

猜你在找的iOS相关文章