Pregunta

Siguiendo iOS6 Eventkit y la nueva configuración de privacidad Estoy usando el siguiente código, que funciona perfectamente en los dispositivos iOS6.

Aún así, me gustaría que el mismo código funcione también para dispositivos con iOS 5.x y desearía no escribir un "mismo código" dos veces, parece incorrecto.

¿Puede alguien ayudar en una solución elegante ?

 EKEventStore *eventStore = [[EKEventStore alloc] init];
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {

// some code 


}];

¿Fue útil?

Solución

Estoy usando esto:

void (^addEventBlock)();

addEventBlock = ^
{
    NSLog(@"Hi!");
};

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

if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
     {
         if (granted)
         {   
             addEventBlock();
         }
         else
         {
             NSLog(@"Not granted");
         }
     }];
}
else
{
    addEventBlock();
}

Creo que debería reducir la duplicación de código.

Otros consejos

Si muestra el evento en un ModalViewController con un EkeVentVentViewController, iOS le muestra automáticamente una vista que debe hacer si se denega el permiso.Entonces, hago esto:

en ViewDidLoad:

eventStore = [[EKEventStore alloc] init];

En el método utilizado para agregar el evento:

if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
    __block typeof (self) weakSelf = self; // replace __block with __weak if you are using ARC
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
     {
         if (granted)
         {   
             [weakSelf performSelectorOnMainThread:@selector(addEventToCalendar) withObject:nil waitUntilDone:YES];
         }
         else
         {
             NSLog(@"Not granted");
         }
     }];
}
else
{
    [self addEventToCalendar];
}

Tengo un archivo eventutil.m con

+(void)proxyForIOS6EventKitToCallFunction:(SEL)function WithViewController:(UIViewController*)viewController {
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    if([app.eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
        // For iOS 6
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:viewController.view animated:YES];
        hud.labelText = @"";
        //invoke requestAccessToEntityType...
        [app.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
            //Handle the response here…
            //Note: If you prompt the user, make sure to call the main thread
            if (granted == YES) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [viewController performSelector:function];
                });
            }
        }];
    }
    else {
        [viewController performSelector:function];
    }
    #pragma clang diagnostic pop
}

y en el controlador de la vista que quiero acceder al calendario I importan el archivo eventutil.h y llame a esta función:

[EventUtil proxyForIOS6EventKitToCallFunction:@selector(displayModifyCalendarAlertView) WithViewController:self];

DisplayModifyCalendarAlerTView es la función que quiero llamar si se da el permiso del calendario (ya sea iOS6 o IOS <6).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top