想要在Google日历中添加活动

时间:2016-10-03 10:17:34

标签: ios objective-c swift google-calendar-api

我想在不使用任何第三方的情况下在Google日历中插入事件。如果有人知道,请帮助我。提前谢谢。

3 个答案:

答案 0 :(得分:1)

你想在谷歌日历中添加一些事件,你可以通过以下google api库来实现这一点:我使用下面的库将事件发布到谷歌日历。

https://github.com/google/google-api-objectivec-client-for-rest

他们提供了使用代码向Google日历添加事件的简要说明。     #pragma mark添加,编辑和删除事件

- (void)addAnEvent {
  // Make a new event, and show it to the user to edit
  GTLRCalendar_Event *newEvent = [GTLRCalendar_Event object];
 newEvent.summary = @"Sample Added Event";
 newEvent.descriptionProperty = @"Description of sample added event";

 // We'll set the start time to now, and the end time to an hour from 
now,
  // with a reminder 10 minutes before
 NSDate *anHourFromNow = [NSDate dateWithTimeIntervalSinceNow:(60 * 60)];

 // Include an offset minutes that tells Google Calendar that these 
dates
  // are for the local time zone.
  NSInteger offsetMinutes = [NSTimeZone localTimeZone].secondsFromGMT / 60;

  GTLRDateTime *startDateTime = [GTLRDateTime dateTimeWithDate:[NSDate date]offsetMinutes:offsetMinutes];
GTLRDateTime *endDateTime = [GTLRDateTime dateTimeWithDate:anHourFromNow
                                       offsetMinutes:offsetMinutes];

newEvent.start = [GTLRCalendar_EventDateTime object];
newEvent.start.dateTime = startDateTime;

 newEvent.end = [GTLRCalendar_EventDateTime object];
 newEvent.end.dateTime = endDateTime;

  GTLRCalendar_EventReminder *reminder = [GTLRCalendar_EventReminder 
   object];
 reminder.minutes = @10;
reminder.method = @"email";

 newEvent.reminders = [GTLRCalendar_Event_Reminders object];
 newEvent.reminders.overrides = @[ reminder ];
 newEvent.reminders.useDefault = @NO;
  [self addEvent:event];}

- (void)addEvent:(GTLRCalendar_Event *)event {
 GTLRCalendarService *service = self.calendarService;
GTLRCalendar_CalendarListEntry *selectedCalendar = [self 
selectedCalendarListEntry];
 NSString *calendarID = selectedCalendar.identifier;

  GTLRCalendarQuery_EventsInsert *query =
     [GTLRCalendarQuery_EventsInsert queryWithObject:event
                                       calendarId:calendarID];
  self.editEventTicket = [service executeQuery:query
                         completionHandler:^(GTLRServiceTicket 
   *callbackTicket,
     GTLRCalendar_Event *event,
                                        NSError *callbackError) {
 // Callback
 self.editEventTicket = nil;
 if (callbackError == nil) {
   [self displayAlert:@"Event Added"
               format:@"Added event \"%@\"",
    event.summary];
   [self fetchSelectedCalendar];
    } else {
    [self displayAlert:@"Add failed"
               format:@"Event add failed: %@", callbackError];
     } }];

 }

- (void)editSelectedEvent {
 // Show the selected event to the user to edit
 GTLRCalendar_Event *eventToEdit = [self selectedEvent];
 if (eventToEdit) {
  EditEventWindowController *controller = [[EditEventWindowController 
alloc] init];
  [controller runModalForWindow:self.window
                        event:eventToEdit
              completionHandler:^(NSInteger returnCode, 
  GTLRCalendar_Event *event) {
              // Callback
              if (returnCode == NSModalResponseOK) {
                [self editSelectedEventWithEvent:event];
              }
            }];
     }
  }

答案 1 :(得分:0)

您想在Google日历中发布或插入一些事件吗?您可以使用以下方式执行此操作:

使用https POST请求,并使用url

发出NSURLSession请求
https://www.googleapis.com/calendar/v3/calendars/calendarId/events

事件发生后,添加您的数据。

答案 2 :(得分:0)

如果您想在特定时间内insert an event,那么您的请求应如下所示。

POST https://www.googleapis.com/calendar/v3/calendars/primary/events?key={YOUR_API_KEY}
{
"end": {
"dateTime": "2016-10-08T8:00:00",
"timeZone": "Asia/Manila"
},
"start": {
"dateTime": "2016-10-08T6:00:00",
"timeZone": "Asia/Manila"
}
}

您需要在请求中指定start.dateTimeend.dateTime

还包括您要使用的timeZone。此API需要您可以在此link中看到的IANA时区格式。