从ios向google日历添加活动 [英] Add event to google calendar from ios

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

问题描述

我按照以下教程从Google日历抓取事件,这是工作正常。
https://developers.google.com/google-apps/calendar / quickstart / ios

I have followed following tutorial to fetch events from google calendar which is working fine. https://developers.google.com/google-apps/calendar/quickstart/ios

现在我在我的iOS应用程序插入事件,因此它可以与网络同步。

Now I am stuck in insert event from my iOS app so that it can be synced with web as well. Please guide me in the right direction or post some sample code.

我在viewDidLoad中使用此代码进行授权

I am using this code for authorization in viewDidLoad

// Initialize the Google Calendar API service & load existing credentials from the keychain if available.
self.service = [[GTLServiceCalendar alloc] init];
self.service.authorizer =
[GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName
                                                      clientID:kClientID
                                                  clientSecret:kClientSecret];

授权似乎很好,因为fetch事件工作正常。但我使用下面的代码添加一个事件

Authorization seems fine because fetch events is working perfectly fine. But I am using following code to add an event

- (void)addAnEvent {
    // Make a new event, and show it to the user to edit
    GTLCalendarEvent *newEvent = [GTLCalendarEvent 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];
    GTLDateTime *startDateTime = [GTLDateTime dateTimeWithDate:[NSDate date]
                                                  timeZone:[NSTimeZone systemTimeZone]];
    GTLDateTime *endDateTime = [GTLDateTime dateTimeWithDate:anHourFromNow
                                                timeZone:[NSTimeZone systemTimeZone]];

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

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

    GTLCalendarEventReminder *reminder = [GTLCalendarEventReminder object];
    reminder.minutes = [NSNumber numberWithInteger:10];
    reminder.method = @"email";

    newEvent.reminders = [GTLCalendarEventReminders object];
    newEvent.reminders.overrides = [NSArray arrayWithObject:reminder];
    newEvent.reminders.useDefault = [NSNumber numberWithBool:NO];

    [self addEvent:newEvent];
}


- (void)addEvent:(GTLCalendarEvent *)event {
    GTLQueryCalendar *query = [GTLQueryCalendar queryForEventsInsertWithObject:event
                                                                calendarId:@"primary"];
    [self.service executeQuery:query
                  delegate:self
         didFinishSelector:@selector(displayAddEventResultWithTicket:finishedWithObject:error:)];
}

- (void)displayAddEventResultWithTicket:(GTLServiceTicket *)ticket
                 finishedWithObject:(GTLCalendarEvents *)events
                              error:(NSError *)error {
    if (error == nil) {
        NSLog(@"I think event has been added successfully!");

    } else {
        NSLog(@"ERROR : %@", error.localizedDescription);
    }
}

但我得到错误响应 (权限不足)

But I am getting the error in response "The operation couldn’t be completed. (Insufficient Permission)"

感谢,

推荐答案

要向日历添加活动,请使用以下方法

To add event to calendar use following method

[GTLQueryCalendar queryForEventsInsertWithObject:yourEventObject calendarId:yourCalendarId]

另请注意,您必须授权范围kGTLAuthScopeCalendar才能具有读/写访问权限。

Also note, that you have to authorize with scope kGTLAuthScopeCalendar to have read/write access.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆