我的应用程序中有一个按钮可以打开 EventEditViewControllerWithEventStore View 。当用户按下“完成”时,该事件将添加到日历中,并且在添加时我会显示通知。但是,我如何判断用户是否取消了呢?截至目前,无论用户按下“完成”还是“取消”,都会显示通知。
这是我目前所拥有的:
- (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];
}
最佳答案
将 EKEventEditViewController
的 delegate
方法设置为 self
,然后添加以下方法来捕获它。
#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)符合 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{
}
}];
}
关于iOS:检测用户是否在事件工具包中取消了 "Add to Calendar"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16996678/