I am trying to create a Google Calendar event using the API in Swift. I am kind of lost at the moment in how to go about that. More specifically creating a GTLRCalendar_Event
object to pass through GTLRCalendarQuery_EventsInsert.query()
. Any way to go about this?
I've written the following code
var newEvent: GTLRCalendar_Event = GTLRCalendar_Event()
newEvent.summary = name
//set GTLRDateTimes
var startTime: GTLRDateTime = GTLRDateTime(date:startTimeObject!, offsetMinutes: offsetMinutes)
var endTime: GTLRDateTime = GTLRDateTime(date:endTimeObject!, offsetMinutes: offsetMinutes)
newEvent.reminders?.useDefault = 0
newEvent.start?.dateTime = startTime
newEvent.end?.dateTime = endTime
let service: GTLRCalendarService = GTLRCalendarService()
let query:GTLRCalendarQuery_EventsInsert = GTLRCalendarQuery_EventsInsert.query(withObject: newEvent, calendarId:"primary")
service.executeQuery(query, completionHandler: {(_ callbackTicket: GTLRServiceTicket, _ event: GTLRCalendar_Event, _ callbackError: Error?) -> Void in
print("executed query")
if callbackError == nil {
print("added")
print(newEvent.summary);
}
else {
print("add failed")
print(callbackError)
}
} as? GTLRServiceCompletionHandler)
I was facing the same problem during the lack of resources at this topic, those are the steps
->configure your app with google calendar account
1-go to https://console.developers.google.com/ add a new project with app bundle id and name
2- go to dashboard click Enable APIS AND SERVICES then choose a calendar API Service and enable It.
3-choose credentials from the side menu and click CREATE CREDENTIALS Link from top of the page and add OAuth Client ID
4-open firebase console https://console.firebase.google.com/u/0/
5- click add project and choose your existing app and continue
6- follow the steps here https://firebase.google.com/docs/ios/setup until download "GoogleService-Info.plist" and add it to your app -> write code to add an event to google calendar
1-follow those steps to add google sign-in https://developers.google.com/identity/sign-in/ios/sign-in?ver=swift
2- // Create an event to the Google Calendar's user
here is the GitHub link https://github.com/emanShedeed/writeEventToGoogleCalendar
I got this to work in Swift 4. I based it on the Java code example that Google has because that one was the most similar. I hope this answers all of your questions. I am sure there is a prettier way to do this, but I don't know it. :)