快速添加Google日历活动

快速添加Google日历活动

本文介绍了快速添加Google日历活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Swift中的API创建Google日历事件。我现在迷失在如何解决这个问题上。更具体地说,创建 GTLRCalendar_Event 对象以通过 GTLRCalendarQuery_EventsInsert.query()。有什么办法解决这个问题?

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?

我编写了以下代码

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)


推荐答案

我在Swift 4中使用了它。我基于Google拥有的Java代码示例,因为那是最相似的示例。我希望这能回答您的所有问题。我敢肯定有一种更漂亮的方法可以做到这一点,但是我不知道。 :)

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
           )
      }

这篇关于快速添加Google日历活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 12:37