Adding a google calendar event in swift

假如想象 提交于 2019-12-23 03:24:26

问题


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)

回答1:


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. :)

//Declares the new event
var newEvent: GTLRCalendar_Event = GTLRCalendar_Event()

//this is setting the parameters of the new event
newEvent.summary = ("Google I/O 2015")
newEvent.location = ("800 Howard St., San Francisco, CA 94103")

//I ran into some problems with the date formatting and this is what I ended with. 

//Start Date. The offset adds time to the current time so if you run the         program at 12:00 then it will record a time of 12:05 because of the 5 minute offset 

let startDateTime: GTLRDateTime = GTLRDateTime(date: Date(), offsetMinutes: 5)
let startEventDateTime: GTLRCalendar_EventDateTime = GTLRCalendar_EventDateTime()
startEventDateTime.dateTime = startDateTime
newEvent.start = startEventDateTime
print(newEvent.start!)

//Same as start date, but for the end date
let endDateTime: GTLRDateTime = GTLRDateTime(date: Date(), offsetMinutes: 50)
let endEventDateTime: GTLRCalendar_EventDateTime = GTLRCalendar_EventDateTime()
endEventDateTime.dateTime = endDateTime
newEvent.end = endEventDateTime
print(newEvent.end!)


let service: GTLRCalendarService = GTLRCalendarService()

//The query 
let query =
GTLRCalendarQuery_EventsInsert.query(withObject: newEvent, calendarId:"Past your calendar ID here this is specific to the calendar you want to edit and can be found under the google calendar settings")

//This is the part that I forgot. Specify your fields! I think this will change when you add other perimeters, but I need to review how this works more. 
query.fields = "id";

//This is actually running the query you just built
self.service.executeQuery(
       query,
       completionHandler: {(_ callbackTicket:GTLRServiceTicket, 
                            _  event:GTLRCalendar_Event,
                            _ callbackError: Error?) -> Void in} 
                           as? GTLRServiceCompletionHandler
           )
      }


来源:https://stackoverflow.com/questions/47801031/adding-a-google-calendar-event-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!