Web Design, Software Development, and More

507-2HAMMER
fax: 702-441-8148
Send EMail

Adding events to the Calendar with iPhone 3.0 SDK

One of the new features of the iPhone 4.0 SDK is access to the calendar. However it is possible to add events today with the 3.0 SDK. How? The trick is to download an ical event using a UIWebView. It will download the event and ask the user if they want to add it to their calendar. It requires you to host a script on a website that outputs a properly formatted ical file and prompts the browser to download it. In fact, this is where the bulk of the code needs to be done. I will show how it can be done using PHP.

The PHP script

The PHP script needs to interpret the string inputs that will come from the client and format it as an ical response. This script will accept about any date and time string format and output the correct time format for the iCal file. Send your time string with a hyphen between the start and end times (like "8am - 10 am") to indicate the duration. If there is no duration, it will set the end time to the same as the start time. Also you can add a test parameter ("test=1") to see the output in a browser easily without it trying to download a file.

<?
$date = strtotime($date);
$timeStart = date("Ymd", $date);
$timeEnd = $timeStart;
if ($time != "(null)") {
    $times = explode("-", $time);
    $start = strtotime($times[0]);
    $timeStart .= "T" . date("His", $start);
    if (count($times) > 1) {
        $end = strtotime($times[1]);
        $timeEnd .= "T" . date("His", $end);
    }
    else {
        $timeEnd = $timeStart;
    }
}
else {
    $timeStart .= "T080000";
    $timeEnd .= "T170000";
}
if ($test == "") {
    header('Content-type: text/calendar');
    header("Content-Disposition:attachment;filename=calendar.ics");
}
else {
    echo "<pre>";
}
?>
BEGIN:VCALENDAR
CALSCALE:GREGORIAN
VERSION:2.0
X-WR-CALNAME:My Calendar
METHOD:PUBLISH
PRODID:-//Apple Inc.//iCal 4.0.1//EN
BEGIN:VTIMEZONE
TZID:America/Chicago
BEGIN:DAYLIGHT
TZOFFSETFROM:-0600
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU
DTSTART:20070311T020000
TZNAME:CDT
TZOFFSETTO:-0500
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:-0500
RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU
DTSTART:20071104T020000
TZNAME:CST
TZOFFSETTO:-0600
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
DTEND;TZID=America/Chicago:<? echo $timeEnd . "\n"; ?>
TRANSP:OPAQUE
DTSTAMP:20100319T230517Z
SUMMARY:<?echo $title;?>
DTSTART;TZID=America/Chicago:<? echo $timeStart . "\n"; ?>
SEQUENCE:1
END:VEVENT
END:VCALENDAR

The iPhone Code

All you need to do on the client side is add a UIWebView subview with the contents of your php script that is actually an ical file. They key here is to use the protocol of webcal:// instead of http://. This is what triggers the client to add it to the calendar..

-(IBAction) addToCalendarButton {
	UIWebView* wv = [[[UIWebView alloc] init] autorelease];
	[self.view addSubview:wv];
	NSString* eventTime = [timeProperty stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding];
	NSString* eventDate = [dateProperty stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding];
	NSString* eventPlace = [placeProperty stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding];
	NSString* urlString = [NSString stringWithFormat:@"webcal://yoursite.com/ics.php?title=%@&date=%@&time=%@", 
		eventPlace, eventDate, eventTime];
	[wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}

The URL request looks something like:

webcal://yoursite.com/ics.php?title=New%20Event&date=June%2026,%202010&time=1:00%20P.M.%20%96%204:00%20P.M.

Your milage may vary, but these are the steps that I found work well. You can see this code in action by downloading the free Alex Ross app from the App Store.

© Copyright 2007-2010 Hammer Technology