iOS:EKEventStore sources / defaultCalendarForNewEvents / calendarsForEntityType all在授权后不返回任何内容

时间:2013-07-31 17:17:22

标签: iphone ios ios6 ekeventstore

我有一个应用程序,我试图帮助走出门。此代码最初是由另一个团队用iOS5编写的。我添加了requestAccessToEntityType:completion:成功运行的调用。但是,在被授予访问权限后,我没有基于实体的source / 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回来了,[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

2 个答案:

答案 0 :(得分:8)

好的,事实证明在viewDidLoad中有一小段代码在[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error代码之前并且试图访问eventStore。这是多余的,不需要发生,我没有注意到它。一旦我删除了这些代码,现在的东西就可以了,所以它确实需要授权,因为即使在授权之后,由于之前的访问,eventStore已经“糟糕”了。

答案 1 :(得分:7)

一旦授予访问权限,重新创建回调中的EKEventStore实例也可以解决问题。