iOS:检测用户是否取消了事件工具包中的“添加到日历”?

时间:2013-06-08 06:14:19

标签: ios objective-c eventkit

我的应用程序中有一个按钮,显示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];


}

1 个答案:

答案 0 :(得分:1)

delegate的{​​{1}}方法设置为EKEventEditViewController,然后添加以下方法来捕获它。

self

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

.h文件

#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];


}

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

@interface EventViewController : UIViewController <EKEventEditViewDelegate> {}