如何在 iPhone 日历上创建提醒和存储提醒

How to create a reminder and store reminder on iPhone Calender

我是编程新手,我制作了一个提醒应用程序,它将事件保存在 iCal 上,现在我想通过项目中的编码删除该事件,我在 iCal 中创建事件的代码如下-

-(void) setReminderInPhone {
NSString *dateWithTime = [NSString stringWithFormat:@"%@ %@",calenderDtl.dateInString,calenderDtl.openTimeUTC];
NSString *mrktName = mrktDtl.marketName;
//dateWithTime = @"2016-04-02 04:10 am";

NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd hh:mm a";
[fmt setAMSymbol:@"am"];
[fmt setPMSymbol:@"pm"];
fmt.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
NSDate *utc = [fmt dateFromString:dateWithTime];

fmt.dateFormat = @"yyyy-MM-dd hh:mm a";
fmt.timeZone = [NSTimeZone systemTimeZone];
NSString *local = [fmt stringFromDate:utc];
NSDate *localDate = [fmt dateFromString:local];
EKEventStore *store = [EKEventStore new];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    if (!granted) { return; }
    EKEvent *event = [EKEvent eventWithEventStore:store];
    event.title = mrktName;
    event.startDate = localDate; //test
    event.endDate = [event.startDate dateByAddingTimeInterval:60*2];
    event.calendar = [store defaultCalendarForNewEvents];
    NSError *err = nil;
    [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
}];
}

尝试按照以下步骤从日历中删除活动。

EKEvent *eventToRemove = [eventStore eventWithIdentifier:eventIdentifier];

if (eventToRemove)
{
    NSError* error = nil;

    [eventStore removeEvent:eventToRemove span:EKSpanThisEvent commit:YES error:&error];
}

您将在 iCalendar 中创建事件时获得 eventIdentifier 的值,如下所示:

//Adding Events to default iOS calendar for Making Reminders
-(NSString *)addEventToCalendar: (NSString *)title withDate:(NSString *)date{

EKEvent *event = [EKEvent eventWithEventStore:eventStore];

event.title = [NSString stringWithFormat:@"Pregnancy Workout Advisor - Event: %@",title];

event.startDate = [dateFormatter dateFromString:date];

event.endDate = [event.startDate dateByAddingTimeInterval:60*60];  //set 1 hour meeting

event.allDay = YES;

[dateFormatter setDateFormat:@"YYYY-MM-dd"];

EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:[NSDate dateWithTimeInterval:9*60*60 sinceDate:[dateFormatter dateFromString:date]]];

[event addAlarm:alarm];

event.calendar = [eventStore defaultCalendarForNewEvents];

NSError *err = nil;

[eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&err];

return event.eventIdentifier;  }

在上述方法中,您将获得要添加到日历的每个事件的 eventIdentifier。将 eventIdentifier 存储到 CoreData/Sqlite,无论什么..

因此,当您需要删除该事件时,只需提供该事件标识符即可。这肯定会从日历中删除。

希望对您有所帮助...

试试这个代码,它对我有用,

EKEventStore *store = [[EKEventStore alloc] init];

            [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
                if (!granted) return;
                EKEvent* eventToRemove = [store eventWithIdentifier:[eventIdendifier objectAtIndex:indexPath.row]];
                if (eventToRemove) {
                    [eventToRemove setCalendar:[store defaultCalendarForNewEvents]];

                    NSError* err = nil;

                    [store removeEvent:eventToRemove span:EKSpanThisEvent commit:YES error:&err];

                    dispatch_async(dispatch_get_main_queue(), ^{

                        [eventIdendifier removeObjectAtIndex:indexPath.row];

                    });
                }
            }];

我已经给了 eventIdentifier 值,它的 NSMutableArray 值。首先你在 Event Store 中添加事件你让每个事件都有每个事件标识符值,它会自动保存到 eventIdentifier 并获取标识符值方法如下,

EKEventStore *store = [EKEventStore new];

    if ([store respondsToSelector:@selector(requestAccessToEntityType:completion:)])
    {
        /* iOS Settings > Privacy > Calendars > MY APP > ENABLE | DISABLE */
        [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
         {
             if ( granted )
             {
                 NSPredicate *predicate = [store predicateForEventsWithStartDate:date1 endDate:[date1 dateByAddingTimeInterval:23.96 * 60.0 * 60.0] calendars:nil];
                 NSMutableArray *eventsVal = (NSMutableArray *)[store eventsMatchingPredicate:predicate];

                 for (int i = 0; i<eventsVal.count; i++) {
                     [eventIdendifier addObject:[[eventsVal objectAtIndex:i] eventIdentifier]];
                 }

             }
         }];

    }

它的谓词从开始日期到结束日期检查,然后获取所有事件的特定日期,如果该值有助于显示事件值、删除事件等,还从 eventIdentifier 值添加对象

希望对您有所帮助