requestAccessToEntityType - 一次还是每次?

时间:2013-01-11 12:22:19

标签: ios objective-c cocoa-touch

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

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

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

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

1 个答案:

答案 0 :(得分:16)

您只需要拨打一次电话:

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
}

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