OStack程序员社区-中国程序员成长平台

标题: ios - 从 ios 将事件添加到谷歌日历 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:41
标题: ios - 从 ios 将事件添加到谷歌日历

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

现在我被困在我的 iOS 应用程序的插入事件中,因此它也可以与网络同步。请指导我正确的方向或发布一些示例代码。

我在 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 事件工作得很好。但我正在使用以下代码添加事件

- (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)addEventGTLCalendarEvent *)event {
    GTLQueryCalendar *query = [GTLQueryCalendar queryForEventsInsertWithObject:event
                                                                calendarId"primary"];
    [self.service executeQuery:query
                  delegate:self
         didFinishSelectorselector(displayAddEventResultWithTicket:finishedWithObject:error];
}

- (void)displayAddEventResultWithTicketGTLServiceTicket *)ticket
                 finishedWithObjectGTLCalendarEvents *)events
                              errorNSError *)error {
    if (error == nil) {
        NSLog(@"I think event has been added successfully!");

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

但我收到错误响应“无法完成操作。(权限不足)”

谢谢,



Best Answer-推荐答案


要将事件添加到日历,请使用以下方法

[GTLQueryCalendar queryForEventsInsertWithObject:yourEventObject calendarId:yourCalendarId]

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

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






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4