5
EKEventStore *eventStore = [[UpdateManager sharedUpdateManager] eventStore];

if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
     {
         if (granted)...

我想请求用户允许将事件添加到他的日历中。授予后,当我想删除一个事件(在应用程序关闭并重新打开后的另一个会话中)时,我是否需要再次请求许可,还是只是需要时间?

如果这是一次性的事情,我可以在第一次午餐时把它放在 ViewDidLoad 中只是为了“摆脱它”吗?

4

1 回答 1

17

您只需要调用一次:

BOOL needsToRequestAccessToEventStore = NO; // iOS 5 behavior
EKAuthorizationStatus authorizationStatus = EKAuthorizationStatusAuthorized; // iOS 5 behavior
if ([[EKEventStore class] respondsToSelector:@selector(authorizationStatusForEntityType:)]) {
    authorizationStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
    needsToRequestAccessToEventStore = (authorizationStatus == EKAuthorizationStatusNotDetermined);
}

if (needsToRequestAccessToEventStore) {
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {            
        if (granted) {
            dispatch_async(dispatch_get_main_queue(), ^{
                // You can use the event store now
            });
        }
    }];
} else if (authorizationStatus == EKAuthorizationStatusAuthorized) {
    // You can use the event store now
} else {
    // Access denied
}

不过,您不应该在第一次发布时这样做。仅在您需要时才请求访问权限,而在用户决定添加事件之前情况并非如此。

于 2013-01-11T12:35:43.440 回答