iOS:检测用户是否已取消";“添加到日历中”;活动中的工具包?
我的应用程序中有一个按钮,用于打开EventEditViewControllerWithEventStore视图。当用户按“完成”时,事件将添加到日历中,并且在添加时我会显示一个通知。但是,如何判断用户是否已取消?到目前为止,无论用户是按“完成”还是“取消”,都会显示通知 这就是我到目前为止所做的:iOS:检测用户是否已取消";“添加到日历中”;活动中的工具包?,ios,objective-c,eventkit,Ios,Objective C,Eventkit,我的应用程序中有一个按钮,用于打开EventEditViewControllerWithEventStore视图。当用户按“完成”时,事件将添加到日历中,并且在添加时我会显示一个通知。但是,如何判断用户是否已取消?到目前为止,无论用户是按“完成”还是“取消”,都会显示通知 这就是我到目前为止所做的: - (IBAction)addEvent:(id)sender { EKEventStore *eventStore = [[EKEventStore alloc] init];
- (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];
}
此外,确保头文件中的接口符合EKEventEditViewDelegate,如下所示:
.h文件
@interface EventViewController : UIViewController <EKEventEditViewDelegate> {}
很好,谢谢你。我只有一个简短的问题。如果用户没有授予应用程序访问日历的权限,我想触发一个操作,但由于某种原因,当我将其放入else{}中时,什么也没有发生。只有NSLog工作。感谢编辑,但我的问题是,在else{}语句中,当我执行NSLog时,它会显示出来,但当我在那里放置UIAlertView时,它不会触发。你知道为什么吗?只有在else语句中,什么都没有发生。
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{
}
}];
}