gpt4 book ai didi

iphone - 在 ios 6 中使用 EventKit 创建新日历时出现 "calendar has no source"错误

转载 作者:可可西里 更新时间:2023-11-01 04:44:15 26 4
gpt4 key购买 nike

我正在 ios 6 中使用 eventkit 框架开发日历应用程序。我正在尝试使用 [self.store respondsToSelector:@selector(requestAccessToEntityType:completion:)] 获得许可 方法并获得访问日历的权限后,我尝试使用 EKSourceTypeLocal 源创建带有标识符的新日历,并将事件添加到新创建的日历中。我在这里遇到的问题是,当我尝试在 iPhone 4s 中运行该应用程序时,它显示“日历没有来源”的错误,并且它不保存我的日历,因此没有事件被添加到日历中。我不知道我在这里做错了什么。请指导我解决这个问题。

我在下面发布我的代码

   - (void)viewDidLoad
{
[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

NSLog(@"in view did load method");


// For iOS 6.0 and later
self.store = [[EKEventStore alloc] init];
if([self.store respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {

[self.store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
// handle access here
dispatch_async(dispatch_get_main_queue(), ^{

if (granted) {

NSLog(@"permission granted");

EKCalendar *myCalendar;

EKSource *myLocalSource = nil;
for (EKSource *calendarSource in self.store.sources) {
if (calendarSource.sourceType == EKSourceTypeLocal) {
myLocalSource = calendarSource;
break;
}
}

if (!myLocalSource)
return;

NSString *mycalIndentifier = [[NSUserDefaults standardUserDefaults] valueForKey:@"my_calendar_identifier"];

if (mycalIndentifier == NULL) {

// Create a new calendar of type Local... save and commit
myCalendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.store];
myCalendar.title = @"New_Calendar";
myCalendar.source = myLocalSource;


NSString *calendarIdentifier = myCalendar.calendarIdentifier;

NSError *error = nil;
[_store saveCalendar:myCalendar commit:YES error:&error];

if (!error) {
NSLog(@"created, saved, and commited my calendar with id %@", myCalendar.calendarIdentifier);

[[NSUserDefaults standardUserDefaults] setObject:calendarIdentifier forKey:@"my_calendar_identifier"];

} else {
NSLog(@"an error occured when creating the calendar = %@", error.description);
error = nil;
}

//create an event

// Create a new event... save and commit
EKEvent *myEvent = [EKEvent eventWithEventStore:_store];
myEvent.allDay = NO;
myEvent.startDate = [NSDate date];
myEvent.endDate = [NSDate date];
myEvent.title = @"Birthday";
myEvent.calendar = myCalendar;
[_store saveEvent:myEvent span:EKSpanThisEvent commit:YES error:&error];

if (!error) {
NSLog(@"the event saved and committed correctly with identifier %@", myEvent.eventIdentifier);
} else {
NSLog(@"there was an error saving and committing the event");
error = nil;
}

EKEvent *savedEvent = [_store eventWithIdentifier:myEvent.eventIdentifier];
NSLog(@"saved event description: %@",savedEvent);

}
else{

myCalendar = [_store calendarWithIdentifier:mycalIndentifier];
}

}
else if(!granted){

NSLog(@"Permission not granted");

}
else{

NSLog(@"error = %@", error.localizedDescription);
}

});

}];
}


}


this is the error I am getting :

Predicate call to calendar daemon failed: Error Domain=EKCADErrorDomain Code=1013 "The operation couldn’t be completed. (EKCADErrorDomain error 1013.)"

not saved = Error Domain=EKErrorDomain Code=14 "Calendar has no source" UserInfo=0x1d5f6950 {NSLocalizedDescription=Calendar has no source}

更新: 我使用此链接解决了问题 try it

最佳答案

问题似乎是在启用 iCloud 时隐藏了本地日历,因此您需要处理这两种情况(启用和未启用 iCloud)。

请参阅此答案以获得可行的解决方案:https://stackoverflow.com/a/15980556/72176

关于iphone - 在 ios 6 中使用 EventKit 创建新日历时出现 "calendar has no source"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17950504/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com