Translate

2016年7月29日 星期五

App Google API Calendar 教學 (note)


  
→建立一個新的授權,若您的使用者砍了你的授權,二次想加回去,直接使用這個就好了
[self presentViewController:[self createAuthController] animated:YES completion:nil];

★授權

Google教學一般只給
kGTLAuthScopeCalendarReadonly

要管理的話(能新增修改編輯)授權權限要改為這個
kGTLAuthScopeCalendar

→建立一個日曆 


    GTLCalendarCalendar*newCalendar=[GTLCalendarCalendar object];

 //新名字就是new Calendar
    newCalendar.summary=@"new Calendar";

    GTLQueryCalendar *queryInsertCalendarList = [GTLQueryCalendar queryForCalendarsInsertWithObject:newCalendar];
    [self.service executeQuery:queryInsertCalendarList
                      delegate:self
             didFinishSelector:nil];


→取得日曆列表



- (void)fetchCalendarList {
    GTLQueryCalendar *query = [GTLQueryCalendar queryForCalendarListList];
    [self.service executeQuery:query
                      delegate:self
             didFinishSelector:@selector(Result_CalendarList:finishedWithObject:error:)];
}

- (void)Result_CalendarList:(GTLServiceTicket *)ticket
   finishedWithObject:(GTLCalendarCalendarList *)list
                error:(NSError *)error {
    
    if (error == nil) {

        for(GTLCalendarCalendarListEntry *cal in list.items){
            //cal.identifier;//判定是否有過
            NSLog(@"%@",cal.summary);
        }
        
    } else {
        [self showAlert:@"Error" message:error.localizedDescription];
    }

}


→刪除授權

 [GTMOAuth2ViewControllerTouch removeAuthFromKeychainForName:kKeychainItemName];



→建立一個事件,此事件活動為一整天

[CalendarID]
need change

- (void)CreateEvent{

    GTLCalendarEvent *calEvent =[GTLCalendarEvent object];
    calEvent.summary= @"test";
    calEvent.descriptionProperty=@"i am test";
    
    calEvent.start = [GTLCalendarEventDateTime object];
    calEvent.end = [GTLCalendarEventDateTime object];
    
    calEvent.start.dateTime=[GTLDateTime dateTimeWithDate:[NSDate dateWithTimeIntervalSinceNow:60*60] timeZone:[NSTimeZone localTimeZone]];


    calEvent.end.dateTime=[GTLDateTime dateTimeWithDate:[NSDate dateWithTimeIntervalSinceNow:60*60+60*60] timeZone:[NSTimeZone localTimeZone]];

    GTLDateTime *startDateTime = [GTLDateTime dateTimeForAllDayWithDate:[NSDate date]
                                                     ];
    
    calEvent.start = [GTLCalendarEventDateTime object];
    calEvent.start.date = startDateTime;
    
    calEvent.end = [GTLCalendarEventDateTime object];
    calEvent.end.date=startDateTime;
    
    
    GTLQueryCalendar *query = [GTLQueryCalendar queryForEventsInsertWithObject:calEvent calendarId:@"[CalendarID]" ];//日曆id
    
    [self.service executeQuery:query
                      delegate:self
             didFinishSelector:@selector(Result_CreateEvent:finishedWithObject:error:)];
}

//結果顯示
- (void)Result_CreateEvent:(GTLServiceTicket *)ticket
           finishedWithObject:(GTLCalendarEvent *)events
                        error:(NSError *)error {
    if (error == nil) {
        //events.identifier; 新增事件iid
    } else {
        [self showAlert:@"Error" message:error.localizedDescription];
    }
}


→編輯一個事件,更新時間為目前時間1小時後的事件

[CalendarID]
[EventID]
need change

   GTLCalendarEvent *calEvent =[GTLCalendarEvent object];
    calEvent.summary= @"change test";
    calEvent.descriptionProperty=@"i am change test";
    
    calEvent.start = [GTLCalendarEventDateTime object];
    calEvent.end = [GTLCalendarEventDateTime object];
    
    calEvent.start.dateTime=[GTLDateTime dateTimeWithDate:[NSDate dateWithTimeIntervalSinceNow:60*60] timeZone:[NSTimeZone localTimeZone]];
    
    
    calEvent.end.dateTime=[GTLDateTime dateTimeWithDate:[NSDate dateWithTimeIntervalSinceNow:60*60+60*60] timeZone:[NSTimeZone localTimeZone]];
    
    NSDate *anHourFromNow = [NSDate dateWithTimeIntervalSinceNow:60*60];
    GTLDateTime *startDateTime = [GTLDateTime dateTimeWithDate:[NSDate date]
                                                      timeZone:[NSTimeZone systemTimeZone]];
    GTLDateTime *endDateTime = [GTLDateTime dateTimeWithDate:anHourFromNow
                                                    timeZone:[NSTimeZone systemTimeZone]];
    
    calEvent.start = [GTLCalendarEventDateTime object];
    calEvent.start.dateTime = startDateTime;
    
    calEvent.end = [GTLCalendarEventDateTime object];
    calEvent.end.dateTime = endDateTime;
    
    
    GTLQueryCalendar *query = [GTLQueryCalendar queryForEventsUpdateWithObject:calEvent
                                                                    calendarId:@"[CalendarID]"                                    eventId:@"[EventID]"];//日曆id
    
    
    [self.service executeQuery:query
                      delegate:self
             didFinishSelector:nil];
}

→刪除一個事件

[CalendarID]
[EventID]
need change

GTLQueryCalendar *query = [GTLQueryCalendar queryForEventsDeleteWithCalendarId:@"[CalendarID]"   eventId:@"[EventID]"];//日曆id

[self.service executeQuery:query delegate:selfdidFinishSelector:nil];

沒有留言:

張貼留言