views:

556

answers:

4

hey

is it possible for my app to programatically add events (with permission from the user) to the iphones calendar?

cheers

w://

+1  A: 

It's not possible in the official iPhone API, so I doubt it's possible via mono touch.

Jasarien
A: 

Not directly possible with the SDK. You might try getting the user to subscribe to an online calendar published from your server, then upload events to that, but you'll probably run into all kinds of syncing issues.

Noah Witherspoon
hmph - major fail :(
cvista
+2  A: 

A lateral thinking way around this is to consider creating an ICS file, then creating an email (which can be done programmatically) and then send it to the user or whomever they want. This is how I'm getting around this issue in my code. The beauty of it is it also doesn't create the sense in the user that your application is going to manage the dates, i.e. if they change something in your application that you'll cancel and rebook things on the fly.

Driss Zouak
i've done this but the calendar files aren't open-able from within iPhone mail - #fail
cvista
good idea though - unless there's a format i'm not using?:)
cvista
+1  A: 

It's available in the iPhone 4 SDK, so in Monotouch. Take a look at EKEventStore and EKEvent. Here's the Apple docs.

Here's an example from a snippets page I keep:

public void CalendarEvents()
{
    EKEventStore store = new EKEventStore();
    EKCalendar calendar = store.DefaultCalendarForNewEvents;

    // Query the event
    if (calendar != null)
    {
        // Searches for every event in the next year
        NSPredicate predicate = store.PredicateForEvents(NSDate.Now,DateTime.Now.AddDays(360),new EKCalendar[] {calendar});

        store.EnumerateEvents(predicate, delegate(EKEvent currentEvent, ref bool stop)
        {
            // Perform your check for an event type
        });


        // Add a new event
        EKEvent newEvent = EKEvent.FromStore(store);
        newEvent.Title = "Lunch at McDonalds";
        newEvent.Calendar = calendar;
        newEvent.StartDate = DateTime.Now.Today;
        newEvent.EndDate = DateTime.Now.Today.AddDays(1);
        newEvent.Availability = EKEventAvailability.Free;
        store.SaveEvent(newEvent, EKSpan.ThisEvent, new IntPtr());
    }
}
Chris S
wooohooo - this is great - but v suprising they took 4 major itterations to get this in!!
cvista
@cvista they're Apple, we have to be grateful for anything they giveth
Chris S