iOS:EKEventStore源/ defaultCalendarForNewEvents / calendarsForEntityType在授权后均不返回任何内容
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS:EKEventStore源/ defaultCalendarForNewEvents / calendarsForEntityType在授权后均不返回任何内容相关的知识,希望对你有一定的参考价值。
我有一个正在尝试帮助您逃脱的应用程序。这段代码最初是由另一个团队使用ios5编写的。我添加了成功运行的requestAccessToEntityType:completion:调用。但是,在被授予访问权限后,我没有基于实体的任何来源/ defaultCalendar或日历。而且我无法创建新的日历。
当调用defaultCalendarForNewEvents时出现此错误
Error Domain=EKCADErrorDomain Code=1013 "The operation couldn’t be completed. (EKCADErrorDomain error 1013.)"
,结果为零。
如果我退出viewController尝试执行此操作然后再返回,则一切正常。如果在收到有关无源的警报后,我继续尝试(不退出viewController),则反复失败。
[我想强调一下,授权调用有效(要求用户访问日历并提供日历,然后再调用以确认授权通过)。我发现的所有其他类似问题均作为解决方案-确保您请求requestAccessToEntityType
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted)
{
//[self performSelectorOnMainThread:@selector(doScheduleActivity:) withObject:activity waitUntilDone:YES];
dispatch_async(dispatch_get_main_queue(), ^{
[self doScheduleActivity:activity];
});
}
else
{ // probably should force the main thread
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Calendar Access Required" message:@"In order to schedule activities, APP needs access to your Calendar. You can change this in your device Settings." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[av show];
});
}
}] ;
granted
返回true,并且[self doScheduleActivity:被调用。以下代码是在例程的开头以及失败所在的位置调用的函数。
EKCalendar *saCalendar;
EKSource *source;
NSError *error;
UIAlertView *alert;
LogDebug(@"eventStore defaultCalendar %@", [eventStore defaultCalendarForNewEvents]);
// validate access to calendar prior to segueing
if ([EKEventStore respondsToSelector:@selector(authorizationStatusForEntityType:)])
{
switch([EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent])
{
case EKAuthorizationStatusAuthorized:
break;
case EKAuthorizationStatusNotDetermined:
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:SA_ALERT_CAPTION_CALENDAR_ACCESS_NOT_DETERMINED message:SA_ALERT_BODY_CALENDAR_ACCESS_NOT_DETERMINED delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
});
return false;
}
case EKAuthorizationStatusDenied:
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:SA_ALERT_CAPTION_CALENDAR_ACCESS_DENIED message:SA_ALERT_BODY_CALENDAR_ACCESS_DENIED delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
});
return false;
}
case EKAuthorizationStatusRestricted:
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:SA_ALERT_CAPTION_CALENDAR_ACCESS_RESTRICTED message:SA_ALERT_BODY_CALENDAR_ACCESS_RESTRICTED delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
});
return false;
}
default:
break;
}
}
// search for application specifc calendar..
saCalendar = nil;
for(EKCalendar *calendar in [eventStore calendarsForEntityType:EKEntityTypeEvent])
{
if([calendar.title isEqualToString:SA_ACTIVITIES_CALENDAR_TITLE])
{
saCalendar = calendar;
break;
}
}
// ..and create from scratch if nonexistent
if(nil == saCalendar)
{
// find local source to hook up new calendar to
for(source in [eventStore sources])
{
if(source.sourceType == EKSourceTypeLocal)
{
break;
}
}
// if could not find local source type, something is wrong
if( source == nil || source.sourceType != EKSourceTypeLocal)
{
alert = [[UIAlertView alloc] initWithTitle:SA_ALERT_CAPTION_CALENDAR_ERROR_NO_SOURCE message:SA_ALERT_BODY_CALENDAR_ERROR delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
return false;
}
// create calendar for applcation, name it, color it and assign source
saCalendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore];
[saCalendar setSource:source];
[saCalendar setTitle:SA_ACTIVITIES_CALENDAR_TITLE];
[saCalendar setCGColor:[[UIColor yellowColor] CGColor]];
// create immediately
error = nil;
if(![eventStore saveCalendar:saCalendar commit:true error:&error])
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:SA_ALERT_CAPTION_CALENDAR_ERROR_CANT_SAVE message:SA_ALERT_BODY_CALENDAR_ERROR delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
});
return false;
}
}
return true;
尽管所有内容都被授权,并且返回[EKEventStore authorizationStatusForEntity:EKEntityType
中的检查返回EKAuthorizationStatusAuthorized,但一切都归零。
在这里出现故障后,如果我退出该视图控制器,然后再返回,则可以正常工作。仅当系统要求用户进行日历访问并且他们回答“确定”时,才会发生这种情况。
感谢您的见解。我一直在调试器中进行调试,并且还进行了printf类型调试,以避免出现任何计时问题等,并且没有发现与我在Apple网站上查看的事件相反的事情。
我的目标设备是6.1.3 iPhone 5
我有一个正在尝试帮助您逃脱的应用程序。这段代码最初是由另一个团队使用iOS5编写的。我添加了requestAccessToEntityType:completion:运行的调用...
[确定,结果证明viewDidLoad中有一小段代码在[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error
代码之前,并试图访问eventStore。它是多余的,不需要发生,我也没有注意到。一旦我删除了此代码,这些东西就可以使用了,因此它确实可以返回到需要授权的地方,因为即使经过授权,由于以前的访问,eventStore也已经是“坏的”。
一旦授予访问权限,也可以通过在回调中重新创建EKEventStore实例来解决此问题。
Swift版本
以上是关于iOS:EKEventStore源/ defaultCalendarForNewEvents / calendarsForEntityType在授权后均不返回任何内容的主要内容,如果未能解决你的问题,请参考以下文章
EKEventStore 导致 SpringBoard 崩溃
EKEventStore 不会按外部或本地标识符返回 Exchange 日历项目