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

标签 iphone ios ios6 ekeventstore

我有一个应用程序,我正试图帮助它走出家门。这段代码最初是由另一个团队针对 iOS5 编写的。我添加了成功运行的 requestAccessToEntityType:completion: 调用。但是,在获得访问权限后,我没有得到任何来源/defaultCalendar 或基于实体的日历。而且我无法创建新日历。

调用 defaultCalendarForNewEvents 时出现此错误

Error Domain=EKCADErrorDomain Code=1013 "The operation couldn't be completed. (EKCADErrorDomain error 1013.)" 结果为 nil。

如果我退出试图执行此操作的 viewController 并返回,一切正常。如果在收到关于没有来源的警报后,我继续尝试(没有退出 viewController),它会反复失败。

我想强调的是,授权调用有效(用户被要求访问日历并提供它,然后调用以确认授权通过)。我发现的所有其他问题都与解决方案相似——确保您请求访问实体类型

[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;

尽管已获得授权,但一切都返回 nil,并且 [EKEventStore authorizationStatusForEntity:EKEntityType 中的检查返回 EKAuthorizationStatusAuthorized

在这里失败后,如果我退出这个 View Controller 并返回,它就可以工作。这只会在系统要求用户访问日历并且用户回复确定时第一次发生。

感谢任何见解。我一直在逐步调试调试器,还进行 printf 类型调试以避免任何类型的计时问题等,并且没有发现任何与我在 Apple 网站上查找的事件内容相反的内容。

我的目标设备是 6.1.3 iPhone 5

最佳答案

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

关于iphone - iOS:EKEventStore sources/defaultCalendarForNewEvents/calendarsForEntityType 在授权后不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17976831/

相关文章:

ios - MPMovieplayercontroller 在流式传输时自动暂停并且不再播放

ios - tableView 对象获取其他对象的属性 - 如何解决它?

ios - 在 iOS 6 中隐藏 TabBar

facebook - iOS Facebook SDK 3.2 通过图路径请求检查权限

iphone - 选项卡栏 Controller :shouldSelectViewController: not being called when delegate is set

iphone - UIView难度

objective-c - 在Objective C中子类化时需要实现的方法

ios - UIApplicationDidBecomeActiveNotification 通知在第一次运行应用程序时未触发

ios - 命令/Applications/Xcode-beta.app/Contents/Developer/usr/bin/actool 失败,退出代码为 255

iphone - 单击 `rel="外部链接时,防止 iPhone 的 webkit 打开新的浏览器窗口(从而退出全屏)