如何使用Google Calendar API iOS Swift创建活动

时间:2016-05-04 07:41:48

标签: ios swift events google-calendar-api

我想开发一个演示应用程序,它将使用我的应用程序创建事件,使用Google Calendar API存储它,然后获取所有数据并提供提醒。我已经引用了这个link来获取数据和进行设置,但我没有得到如何创建事件。任何人都可以指导我如何做到这一点?我搜索了许多使用iOS创建活动的内容,但我没有对Google Calendar API有任何用处,我发现了使用EventKit框架的所有内容。

请帮帮我。 谢谢。

2 个答案:

答案 0 :(得分:1)

我找到了一个教程:"iOS SDK: Working with Google Calendars",在本教程中,他们提供了有关下载日历以及创建包含说明和日期/时间的新事件的见解。

  

关于我们的app结构,基本视图将是一个表视图,其中包含三个用于设置以下数据的部分:

     
      
  • 事件描述
  •   
  • 活动日期/时间
  •   
  • 目标日历
  •   

添加事件(目标C)的代码示例:

// Create the URL string of API needed to quick-add the event into the Google calendar.
// Note that we specify the id of the selected calendar.
NSString *apiURLString = [NSString stringWithFormat:@"https://www.googleapis.com/calendar/v3/calendars/%@/events/quickAdd",
                          [_dictCurrentCalendar objectForKey:@"id"]];

// Build the event text string, composed by the event description and the date (and time) that should happen.
// Break the selected date into its components.
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit
                                                 fromDate:_dtEvent];

if (_isFullDayEvent) {
    // If a full-day event was selected (meaning without specific time), then add at the end of the string just the date.
    _strEventTextToPost = [NSString stringWithFormat:@"%@ %d/%d/%d", _strEvent, [dateComponents month], [dateComponents day], [dateComponents year]];
}
else{
    // Otherwise, append both the date and the time that the event should happen.
    _strEventTextToPost = [NSString stringWithFormat:@"%@ %d/%d/%d at %d.%d", _strEvent, [dateComponents month], [dateComponents day], [dateComponents year], [dateComponents hour], [dateComponents minute]];
}

// Show the activity indicator view.
[self showOrHideActivityIndicatorView];

// Call the API and post the event on the selected Google calendar.
// Visit https://developers.google.com/google-apps/calendar/v3/reference/events/quickAdd for more information about the quick-add event API call.
[_googleOAuth callAPI:apiURLString
       withHttpMethod:httpMethod_POST
   postParameterNames:[NSArray arrayWithObjects:@"calendarId", @"text", nil]
  postParameterValues:[NSArray arrayWithObjects:[_dictCurrentCalendar objectForKey:@"id"], _strEventTextToPost, nil]];

我认为您可以从iOS Quickstart Google开始的内容中实现这一点。

我希望这会有所帮助。古德勒克:)

答案 1 :(得分:0)

首先,你必须创建一个公共API,get是关键。并使用以下代码

设置其URL
let url = NSURL(string: "https://www.googleapis.com/calendar/v3/calendars/email.gmail.com/events?maxResults=15&key=APIKey-here")

它适合你