Skip to main content

How can I programmatically create an iCal event in the default calendar?



How can I use Objective-C to programmatically create an iCal event in the default calendar? I want to check whether the event already exists and set the button state accordingly.




Comments

  1. An example of how to programmatically create an iCAL event in the default calendar, using Objective-C. The code checks if the event already exists, and sets the button state accordingly. Here is the code by @codeburger:

    -(void)initCalendar {

    // An array of 1 dictionary object, containing START and END values.
    NSMutableArray* pvDict = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:PV_URL ]];

    // Check if the private view event already exists in the default calendar.
    // Then set the calendar button state.

    // Get a entry point to the Calendar database.
    self.store = [[EKEventStore alloc ] init ];

    // Get an array of all the calendars.
    NSArray *calendars = store.calendars;

    // Get the default calendar, set by the user in preferences.
    EKCalendar *defaultCal = store.defaultCalendarForNewEvents;

    // Find out if this calendar is modifiable.
    BOOL isDefaultCalModifiable = defaultCal.allowsContentModifications ;

    // Create an event in the default calendar.

    self.event = [ EKEvent eventWithEventStore:store ];

    self.event.title = CHELSEA_SPACE ;
    self.event.location = CHELSEA_ADDRESS ;

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyy-MM-dd HH:mm:ss.S"];

    NSString *startString = [[ pvDict objectAtIndex:0] objectForKey:@"starts" ];
    NSDate *dateStart = [dateFormatter dateFromString:startString];

    NSString *endString = [[ pvDict objectAtIndex:0] objectForKey:@"ends" ];
    NSDate *dateEnd = [dateFormatter dateFromString:endString];

    self.event.startDate = dateStart;
    self.event.endDate = dateEnd;

    self.event.calendar = defaultCal ;

    // Alternative code to add 2.5 hours to start time.
    // [[NSDate alloc] initWithTimeInterval:9000 sinceDate:event.startDate];

    // Search for events which match this date/time start and end.
    // Compare the matched events by event TITLE.

    NSPredicate *predicate = [store predicateForEventsWithStartDate:event.startDate
    endDate:event.endDate calendars:calendars];

    NSArray *matchingEvents = [store eventsMatchingPredicate:predicate];

    self.calendarButton.enabled = NO;

    if( ! isDefaultCalModifiable ) {
    // The default calendar is not modifiable
    return ;
    }

    if ( [ matchingEvents count ] > 0 ) {

    // There are already event(s) which match this date/time start and end.
    // Check if this event is the PV

    EKEvent *anEvent;
    int j;

    for ( j=0; j < [ matchingEvents count]; j++) {

    anEvent = [ matchingEvents objectAtIndex:j ] ;
    if([ CHELSEA_SPACE isEqualToString: anEvent.title ]) {
    // PV event already exists in calendar.
    return;
    }
    }
    [ anEvent release ];
    }

    self.calendarButton.enabled = YES;

    [ pvDict release ];
    }

    -(void)addEventToCalendar:(id)sender {

    NSError *error;
    BOOL saved = [self.store saveEvent:self.event span:EKSpanThisEvent error:&error];

    NSLog(@"Saved calendar event = %@\n", (saved ? @"YES" : @"NO"));
    self.calendarButton.enabled = NO;

    }


    I've seen this question with no answer and felt like it should be edited giving full credit to @codeburger.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.