我正在尝试使用Swift中的API创建Google Calendar事件。我现在有点失去了如何解决这个问题。更具体地说,创建一个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)
答案 0 :(得分:3)
我在Swift 4中使用它。我基于谷歌的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
)
}
答案 1 :(得分:0)
在此主题上资源匮乏的时候,我遇到了同样的问题,这些步骤是
->使用Google日历帐户配置您的应用
1-转到https://console.developers.google.com/,添加一个具有应用程序包ID和名称的新项目
2-转到仪表板,单击“启用API和服务”,然后选择一个日历API服务并启用它。
3-从侧面菜单中选择凭据,然后点击页面顶部的CREATE CREDENTIALS链接并添加OAuth客户端ID
4-open Firebase控制台https://console.firebase.google.com/u/0/
5-单击添加项目,然后选择您现有的应用并继续
6-按照此处的https://firebase.google.com/docs/ios/setup步骤进行操作,直到下载“ GoogleService-Info.plist”并将其添加到您的应用中 ->编写代码以将事件添加到Google日历
1-按照这些步骤添加Google登录 https://developers.google.com/identity/sign-in/ios/sign-in?ver=swift
2- //为Google日历的用户创建一个事件
func addEventoToGoogleCalendar(summary : String, description :String, startTime : String, endTime : String) {
let calendarEvent = GTLRCalendar_Event()
calendarEvent.summary = "\(summary)"
calendarEvent.descriptionProperty = "\(description)"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy HH:mm"
let startDate = dateFormatter.date(from: startTime)
let endDate = dateFormatter.date(from: endTime)
guard let toBuildDateStart = startDate else {
print("Error getting start date")
return
}
guard let toBuildDateEnd = endDate else {
print("Error getting end date")
return
}
calendarEvent.start = buildDate(date: toBuildDateStart)
calendarEvent.end = buildDate(date: toBuildDateEnd)
let insertQuery = GTLRCalendarQuery_EventsInsert.query(withObject: calendarEvent, calendarId: "primary")
service.executeQuery(insertQuery) { (ticket, object, error) in
if error == nil {
print("Event inserted")
} else {
print(error)
}
}
}
// Helper to build date
func buildDate(date: Date) -> GTLRCalendar_EventDateTime {
let datetime = GTLRDateTime(date: date)
let dateObject = GTLRCalendar_EventDateTime()
dateObject.dateTime = datetime
return dateObject
}
// Helper for showing an alert
func showAlert(title : String, message: String) {
let alert = UIAlertController(
title: title,
message: message,
preferredStyle: UIAlertController.Style.alert
)
let ok = UIAlertAction(
title: "OK",
style: UIAlertAction.Style.default,
handler: nil
)
alert.addAction(ok)
present(alert, animated: true, completion: nil)
}
这是GitHub链接https://github.com/emanShedeed/writeEventToGoogleCalendar