Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requestAccessToEntityType - once or every time?

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

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

I want to ask the user for permission to add an event to his calendar. After it's granted do I need to ask for permission again when I want for example to remove an event (in another session after the app was closed and reopened) or is it just a want time thing?

If it's a one time thing, can I just put it in ViewDidLoad at first lunch just to "get rid of it" ?

like image 551
Segev Avatar asked Jan 11 '13 12:01

Segev


1 Answers

You only need to call it once:

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
}

You shouldn't do that on the first launch, though. Only request access when you need it and that isn't the case just until the user decides to add an event.

like image 160
Fabian Kreiser Avatar answered Sep 24 '22 13:09

Fabian Kreiser