Translate

顯示具有 Calendar 標籤的文章。 顯示所有文章
顯示具有 Calendar 標籤的文章。 顯示所有文章

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];

App Google API Calendar 教學


★Google官方教學~快速建立跟檢視(本篇主要照這個改)
https://developers.google.com/google-apps/calendar/quickstart/ios?hl=zh-tw

Google官方教學~指南(如果注意到的話,也跟上面其實是同一頁面)
https://developers.google.com/google-apps/calendar/v3/reference/?hl=zh-tw

中文的API介紹
https://jaygoo.hackpad.com/google-Calendar-API-E5pl6UbGOvN#:h=4.取得日曆活動


下面是補充用的...
Google官方熱門產品查詢
https://developers.google.com/products/?hl=zh-tw


看帳戶的授權
https://security.google.com/settings/security/permissions

API申請憑證
https://console.developers.google.com/apis?pli=1


主要是照著官方教學做的...


1.剛開始先去申請憑證https://console.developers.google.com/apis?pli=1
建立一個專案,建立好後,再選【資料庫】(在左邊工具列),找右邊選項你要開的API項目,日曆在最右邊第二個位置,點開連結後,按下上方的啟用,就可以了。

2.接下來啟用以後,要建立憑證,按下【前往憑證】,重點來了,接下來呼叫來源下拉選擇iOS裝置,然後下方的使用者資料也要勾選後,再按下【我需要哪些憑證】…

3.然後你可以進行名稱命名,聯繫ID你可以隨便打一組,不過如果你有APP的話,還是用APP 的 SKU ID為主…

4.然後建立好,其他就看你要怎麼顯示授權頁面了…最主的是最後顯示出來的Client ID很重要,要留著,等一下程式要用到,不然也可以下載下來,裡面的檔案用筆記本開啟,把ID拿出來也可以…


~~~~~~~~~~~~~~~~


1.開你的XCode吧,建立一個專案,單一頁面,然後再開你的終端機(這裡要先安裝過CocoaPods,不會裝的話看一下這裡http://bahole.blogspot.tw/2016/07/cocoapods_28.html

2.然後一樣終端機的檔案位置要在你的專案底下 ,用pwd指令看看你的目錄在哪,確認位置正確後,在終端機直接貼google給你的教學...(我先照copy,有可能未來會變)

ps
QuickstartApp→是google的專案名,如果你的專案名不一樣要記得改下面內文的文字,改成跟你的專案名稱一樣
cat << EOF > Podfile &&
platform :ios, '7.0'
target 'QuickstartApp' do
    pod 'GoogleAPIClient/Calendar', '~> 1.0.2'
    pod 'GTMOAuth2', '~> 1.1.0'
end
EOF
pod install &&
open QuickstartApp.xcworkspace


3.如果順利的話,他就會幫你裝好了Google的檔案

4.它會直接打開你的專案,接下來依然無腦照著檔案名稱copy下去,除了
Client ID你要換成你自已的,應該就能執行成功了,問你要不要授權~然後就可以顯示最近前10筆活動

~~~~~~~~~~~~~~~~



#import <UIKit/UIKit.h>
#import "GTMOAuth2ViewControllerTouch.h"
#import "GTLCalendar.h"
@interface ViewController : UIViewController
@property (nonatomic, strong) GTLServiceCalendar *service;
@property (nonatomic, strong) UITextView *output;
@end


#import <UIKit/UIKit.h>
#import "GTMOAuth2ViewControllerTouch.h"
#import "GTLCalendar.h"
@interface ViewController : UIViewController
@property (nonatomic, strong) GTLServiceCalendar *service;
@property (nonatomic, strong) UITextView *output;
@end