The TelerikNEXT sample shows you how to add events to the user's default calendar. I thought any app that want to write to the calendar should allow the user to decide which of their calendars to add an event to. The below code is in a modal view that I display in response to a tap on a button labeled "Add to Calendar" when the user is viewing an event listing. While the code I'm posing here doesn't actually do anything with the information it retrieves, the real code pushes the calendar ID and title as a JSON object into an observable array that is bound to a ListView.
var platform = require("platform");
var appModule = require("application");
if (platform.device.os === platform.platformNames.android) {
var projection = java.lang.reflect.Array.newInstance(java.lang.String.class, 3);
projection[0] = "_id";
projection[1] = "calendar_displayName";
projection[2] = "calendar_access_level";
var cals = android.net.Uri.parse("content://com.android.calendar/calendars");
var contentResolver = appModule.android.foregroundActivity.getApplicationContext().getContentResolver();
var managedCursor = contentResolver.query(cals, projection, null, null, null);
if (managedCursor !== null) {
while (managedCursor.moveToNext()) {
var idCol = managedCursor.getColumnIndex(projection[0]);
var nameCol = managedCursor.getColumnIndex(projection[1]);
var permsCol = managedCursor.getColumnIndex(projection[2]);
if (managedCursor.getString(permsCol) === "700") { // select from only those the user owns
console.log("Calendar: " + managedCursor.getString(nameCol) + ", ID: " + managedCursor.getString(idCol));
// Do something with the data
// perhaps: someObservableArray.push({ id: managedCursor.getString(idCol), title: managedCursor.getString(nameCol) });
}
}
}
}
else if (platform.device.os === platform.platformNames.ios) {
var store = EKEventStore.new();
store.requestAccessToEntityTypeCompletion(EKEntityTypeEvent, function (granted, error) {
if (!granted) {
// Close the modal dialog now
return;
}
var cals = store.calendarsForEntityType(EKEntityTypeEvent); //store.sources;
for (var i = 0; i < cals.count; i++) {
var cal = cals.objectAtIndex(i);
if (cal && cal.allowsContentModifications) { // Got calendar and it's writable
console.log("id=" + cal.calendarIdentifier + ", title=" + cal.title + ", canmodify=" + cal.allowsContentModifications);
// Do something with the data
// perhaps: someObservableArray.push({ id: cal.calendarIdentifier, title: cal.title });
}
}
});
}