蓝天,小湖,湖水中一方小筑

Show alert box in calendar operations of iOS6

With the new permission schema in iOS6, app should acquire authentication before accessing calendar. So the code for calendar operation should be surrounded like this:

EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event  = [EKEvent eventWithEventStore:eventStore];

[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    if (granted) {
        // Calendar operation
    }
}];

As you can see, the code for calendar operation is written in a block.

After the operation completed, you may want to show customer an alert box to say the operation is done. But, showing a UIAlertView object in block will not perform expected action. The reason is because of the event loop. I am still studying it, so will update it later.

The correct way to show an alert box is using dispatch_async function to put the operation block to main queue, which looks like this:

[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    if (granted) {
        // Calendar operation
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView *alert = [[UIAlertView alloc]
                                  initWithTitle:@"Event Added"
                                  message:nil
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
            [alert show];
        }

    }
}];