1

我正在使用以下两种方法在设备日历中创建事件,事件是我的 managedObject 类。

+ (void)createCalendarEvents:(NSArray *)eventsArray
{

    EKEventStore *eventStore = [[SharedLoader sharedInstance] eventStore];
    NSLog(@"eventStore: %@",eventStore);

    BOOL needsToRequestAccessToEventStore = NO; // iOS 5 behavior
    if ([[EKEventStore class] respondsToSelector:@selector(authorizationStatusForEntityType:)])
        needsToRequestAccessToEventStore = ([EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent] == EKAuthorizationStatusNotDetermined);

    if (needsToRequestAccessToEventStore) {
        [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
            if (granted) {

                dispatch_async(dispatch_get_main_queue(), ^{

                for(Events *eventObject in eventsArray)
                [self addEventToCalendar:eventObject forEventStoreObject:eventStore];
                    NSError *commitError;
                    if([eventStore commit:&commitError])
                    {
                        NSLog(@"Events commited to device calendar");
                    }

                    else
                    {
                        NSLog(@"Events could not be commited to device calendar: %@",[commitError localizedDescription]);
                    }
                });
            }
        }];
    }

    else {

        dispatch_async(dispatch_get_main_queue(), ^{

        for(Events *eventObject in eventsArray)
        [self addEventToCalendar:eventObject forEventStoreObject:eventStore];
            NSError *commitError;
            if([eventStore commit:&commitError])
            {
                NSLog(@"Events commited to device calendar");
            }

            else
            {
                NSLog(@"Events could not be commited to device calendar: %@",[commitError localizedDescription]);
            }

 });
        }


}


+ (BOOL)addEventToCalendar:(Events *)eventObject forEventStoreObject:(EKEventStore *)eventStore
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormatter setLocale:[NSLocale currentLocale]];

    NSString *date =[NSString stringWithFormat:@"%@", eventObject.eventStartDate];


    NSPredicate *predicateForEventsOndate = [eventStore predicateForEventsWithStartDate: eventObject.eventStartDate endDate:[[NSDate alloc] initWithTimeInterval:600 sinceDate:[dateFormatter dateFromString:date]] calendars:nil]; // nil will search through all calendars


    NSArray *eventsOnDate = [eventStore eventsMatchingPredicate:predicateForEventsOndate];


    BOOL eventExists = NO;

    EKEvent *existingEvent = nil;

    for (EKEvent *eventToCheck in eventsOnDate) {
        if ([eventToCheck.title isEqualToString:eventObject.eventTitle]) {
            eventExists = YES;
            existingEvent=eventToCheck;
            break;
        }
    }

    if (eventExists == NO) {

        EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
        event.title =[NSString stringWithFormat:@"%@",eventObject.eventTitle];
        NSLog(@"calendar event: %@", event.title);
        NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
        [tempFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        [tempFormatter setLocale:[NSLocale currentLocale]];
        NSString *dateandtime =[NSString stringWithFormat:@"%@",eventObject.eventStartDate];
        NSLog(@"dateandtime: %@",dateandtime);
        NSString *dateandtimeend =[NSString stringWithFormat:@"%@",eventObject.eventEndDate];
        NSLog(@"dateandtime: %@",dateandtimeend);

        event.location = [NSString stringWithFormat:@"%@",eventObject.eventVenue];;
        event.startDate =[tempFormatter dateFromString:dateandtime];
        event.endDate   = [tempFormatter dateFromString:dateandtimeend];
        event.notes =eventObject.eventPurpose;


        /*[event addAlarm:[EKAlarm alarmWithAbsoluteDate:event.startDate]];
        [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -60.0f * 24]];
        [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -15.0f]];*/

        [event setCalendar:[eventStore defaultCalendarForNewEvents]];

        NSError *err;
        if([eventStore saveEvent:event span:EKSpanThisEvent error:&err])
        {
            NSLog(@"New Event Saved Successfully");
            return YES;
        }

        else
        {
            NSLog(@"New Event could not be saved: %@ %@",[err localizedDescription],err);
            return NO;


        }
    }
    else
    {
        existingEvent.title =[NSString stringWithFormat:@"%@",eventObject.eventTitle];
        NSLog(@"calendar event: %@", existingEvent.title);
        NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
        [tempFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        [tempFormatter setLocale:[NSLocale currentLocale]];
        NSString *dateandtime =[NSString stringWithFormat:@"%@",eventObject.eventStartDate];
        NSLog(@"dateandtime: %@",dateandtime);
        NSString *dateandtimeend =[NSString stringWithFormat:@"%@",eventObject.eventEndDate];
        NSLog(@"dateandtime: %@",dateandtimeend);

        existingEvent.location = [NSString stringWithFormat:@"%@",eventObject.eventVenue];;
        existingEvent.startDate =[tempFormatter dateFromString:dateandtime];
        existingEvent.endDate   = [tempFormatter dateFromString:dateandtimeend];
        existingEvent.notes =eventObject.eventPurpose;

         /*[existingEvent addAlarm:[EKAlarm alarmWithAbsoluteDate:existingEvent.startDate]];
         [existingEvent addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -60.0f * 24]];
         [existingEvent addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -15.0f]];*/

        [existingEvent setCalendar:[eventStore defaultCalendarForNewEvents]];

        NSError *err;
        if([eventStore saveEvent:existingEvent span:EKSpanThisEvent error:&err])
        {
            NSLog(@"Old Event Updated & Saved Successfully");
            return YES;
        }

        else
        {
            NSLog(@"Existing Event could not be saved: %@",[err localizedDescription]);
            return NO;


        }
    }

}

我的事件已成功保存,并且我在设备日历上也看到了它们,但事件发生在 2001 年,并且在事件列表中进行了一些滚动后,事件消失了。在这方面需要帮助。

4

0 回答 0