Search code examples
iosjsoncalendartitaniumappcelerator

Adding event to calendar through Json ios titanium


I am trying to add events to ios calendar through a json file timetable. It adds events to the calendar but i don't want to add the events with duplicate entry. If date already has exisisting event it should not add it. what changes can be made in this this code?

var fileName = 'timetable.json';
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory, fileName);

 if (file.exists()) {
var data =  file.read();
var timetab = JSON.parse(data); 
Ti.API.info("json file" + timetab.timetable[0].events);
 }

for(var i=0; i<timetab.timetable.length;i++)
 {
 var endDate = new Date(timetab.timetable[i].date);

endDate.setHours(endDate.getHours()+3);


// Add event to our calendar.

Ti.Calendar.addEvent({
title: timetab.timetable[i].events,

startDate: new Date(timetab.timetable[i].date),
endDate: endDate,
location: "At home",
 note: "A note",
alarm: {
    offset: -900
 },

 identifier: Ti.Calendar.identifier,
type:"private",
attendees: "xyz",
organizer: "abc"
 });
}

Solution

  • You should first get All events of the date on which you are going to add Events, you can use getEventsInDate Method for this, just check if the events are already present in the calendar, if not add them.

    Hope it helps.