gpt4 book ai didi

ios - 快速添加谷歌日历事件

转载 作者:搜寻专家 更新时间:2023-11-01 06:30:42 25 4
gpt4 key购买 nike

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

我写了下面的代码

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 代码示例,因为那个代码示例最相似。我希望这能回答你所有的问题。我确信有一种更漂亮的方法可以做到这一点,但我不知道。 :)

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

关于ios - 快速添加谷歌日历事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47801031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com