0

我的应用程序中有一个按钮,可以调出 EventEditViewControllerWithEventStore 视图。当用户按下完成时,该事件将添加到日历中,并在添加时显示通知。但是,我怎么知道用户是否取消了?到目前为止,无论用户是否按下完成或取消,都会显示通知。

这是我到目前为止所拥有的:

- (IBAction)addEvent:(id)sender {

    EKEventStore *eventStore = [[EKEventStore alloc] init];
    if([eventStore respondsToSelector: @selector(requestAccessToEntityType:completion:)]) {

        [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL permissionGranted, NSError *error) {

            if (permissionGranted){

                NSLog(@"PERMISSION GRANTED.");

                [self performSelectorOnMainThread:@selector(presentEventEditViewControllerWithEventStore:) withObject:eventStore waitUntilDone:NO];
            }

            else {

                NSLog(@"NO PERMISSION.");

                [TSMessage showNotificationInViewController:self withTitle:@"Oops! Permission Required" withMessage:@"In order to use this feature, please enable calendar access for this app under settings." withType:TSMessageNotificationTypeWarning withDuration:TSMessageNotificationDurationAutomatic withCallback:nil atPosition:TSMessageNotificationPositionTop];    

            }
        }];
    }

    else {

        // If device is > 6.0
        [self presentEventEditViewControllerWithEventStore:eventStore];
    }
}

- (void)presentEventEditViewControllerWithEventStore:(EKEventStore*)eventStore
{
    EKEventEditViewController *vc = [[EKEventEditViewController alloc] init];
    vc.eventStore = eventStore;

    EKEvent* event = [EKEvent eventWithEventStore:eventStore];

    //Pre-populated Info
    event.title = @"Event title";
    event.startDate = [NSDate date];
    event.endDate = [NSDate date];
    event.URL = [NSURL URLWithString:@"http://example.com"];
    event.notes = @"Event Notes Can Go Here.";
    event.allDay = YES;
    event.location = @"Earth";
    event.availability = EKEventAvailabilityTentative;
    vc.event = event;
    vc.editViewDelegate = self;

    [self presentViewController:vc animated:YES completion:nil];
}

#pragma EKEventEditViewDelegate

- (void)eventEditViewController:(EKEventEditViewController*)controller
          didCompleteWithAction:(EKEventEditViewAction)action {


    [controller dismissViewControllerAnimated:YES completion:nil];

    [self performSelector:@selector(eventAddedSuccessfully) withObject:nil afterDelay:0.5];

}

#pragma TSMessages

- (void)eventAddedSuccessfully {

    NSLog(@"Event was successfully added.");


    [TSMessage showNotificationInViewController:self withTitle:@"Success!" withMessage:@"The event was added to your calendar." withType:TSMessageNotificationTypeSuccess withDuration:TSMessageNotificationDurationAutomatic withCallback:nil atPosition:TSMessageNotificationPositionTop];


}
4

1 回答 1

1

delegate您的方法设置EKEventEditViewControllerself,然后添加以下方法来捕获它。

#pragma mark -
#pragma mark EKEventEditViewDelegate

// Overriding EKEventEditViewDelegate method to update event store according to user actions.
- (void)eventEditViewController:(EKEventEditViewController *)controller 
          didCompleteWithAction:(EKEventEditViewAction)action {

    NSError *error = nil;
    //EKEvent *thisEvent = controller.event;

    switch (action) {
        case EKEventEditViewActionCanceled:
            // Edit action canceled, do nothing. 
            break;

        case EKEventEditViewActionSaved:
            // When user hit "Done" button, save the newly created event to the event store, 
            // and reload table view.
            // If the new event is being added to the default calendar, then update its 
            // eventsList.
            [controller.eventStore saveEvent:controller.event span:EKSpanThisEvent error:&error];
            break;

        case EKEventEditViewActionDeleted:
            // When deleting an event, remove the event from the event store, 
            // and reload table view.
            // If deleting an event from the currenly default calendar, then update its 
            // eventsList.
                        break;

        default:
            break;
    }
    // Dismiss the modal view controller
    [controller dismissViewControllerAnimated:YES completion:nil];


}

另外,请确保您在头文件中的界面符合 EKEventEditViewDelegate,如下所示:

.h 文件

@interface EventViewController : UIViewController <EKEventEditViewDelegate> {}

以下是如何检查您是否被授予添加/修改事件的权限:

EKEventStore *store = [[EKEventStore alloc] init];    
if([store respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
            //Access not granted-------------
            if(!granted){

            }

            //Access granted
            }else{

            }
    }];
}
于 2013-06-08T06:25:26.260 回答