Created
June 28, 2021 09:26
-
-
Save ewanharris/53c5ad8033aeef02d61e957bf40c72f9 to your computer and use it in GitHub Desktop.
Calendar sample iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const win = Ti.UI.createWindow(); | |
function createEvent() { | |
let calendarId; | |
for (const calendar of Ti.Calendar.allCalendars) { | |
Ti.API.info(calendar.name, calendar.id); | |
// This probably is not good enough for an app, the calendar most likely should be selected by the user | |
if (calendar.name === 'Calendar') { | |
calendarId = calendar.id | |
} | |
} | |
if (!calendarId) { | |
console.error('Failed to find calendar id for "Calendar"'); | |
return; | |
} | |
const begin = new Date('2021-06-29T12:00:00'); | |
const end = new Date('2021-06-29T12:30:00'); | |
const calendar = Ti.Calendar.getCalendarById(calendarId); | |
const sampleEvent = calendar.createEvent({ | |
title: 'Sample Event', | |
notes: 'This is a test event which has some values assigned to it.', | |
begin, | |
end | |
}); | |
// Now create an alert for the event, then add it to the alerts array | |
const sampleAlert = sampleEvent.createAlert({ | |
absoluteDate: new Date('2021-06-29T11:00:00') | |
}); | |
sampleEvent.alerts = [ sampleAlert ]; | |
// Now save the event to persist it to the calendar | |
sampleEvent.save(); | |
console.log(`Created event ${sampleEvent.id}`); | |
} | |
win.addEventListener('click', () => { | |
if (Ti.Calendar.hasCalendarPermissions()) { | |
return createEvent() | |
} | |
Ti.Calendar.requestCalendarPermissions((e) => { | |
if (!e.success) { | |
console.error('Permissions not granted'); | |
return; | |
} | |
return createEvent(); | |
}); | |
}); | |
win.open() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment