ㄷㅣㅆㅣ's Amusement
iOS calendar program tutorial 본문
iOS calendar program tutorial
반응형
    
    
    
  iOS에서 캘린더에 접근하는 방법.
1. header
  - @import EventKit;
2. Getting Permission.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24  | - (void)requestAccessToEvents:(void (^)(BOOL bGranted))complete {     BOOL needsToRequestAccessToEventStore = NO;     EKAuthorizationStatus authorizationStatus = EKAuthorizationStatusAuthorized;     if ([[EKEventStore class] respondsToSelector:@selector(authorizationStatusForEntityType:)]) {         authorizationStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];         needsToRequestAccessToEventStore = (authorizationStatus == EKAuthorizationStatusNotDetermined);     }     if (needsToRequestAccessToEventStore) {         [self.eventManager.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {             if (granted) {                 NSLog(@"event just granted");                 complete(YES);             }         }];     } else if (authorizationStatus == EKAuthorizationStatusAuthorized) {         NSLog(@"event granted");         complete(YES);     } else {         // TODO : Access denied popup         NSLog(@"event access denied");         complete(NO);     } }  | cs | 
3. Getting Calendars
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18  | - (NSArray *)getSuitableEventCalendars{     // get all calendars     // 모든 캘린더를 가져온다.     NSArray *allCalendars = [self.eventStore calendarsForEntityType:EKEntityTypeEvent];     // extract local calendars and GMail calendars only.     // 그중에서 로컬캘린더와 구글 캘린더만 추출.     NSMutableArray *suitableCalendars = [NSMutableArray array];     for (int i=0; i<allCalendars.count; i++) {         EKCalendar *currentCalendar = [allCalendars objectAtIndex:i];         if ((currentCalendar.type == EKCalendarTypeLocal)             || ([currentCalendar.source.title isEqualToString:@"GMail"])){             [suitableCalendars addObject:currentCalendar];         }     }     return (NSArray *)suitableCalendars; }  | cs | 
3. Getting Events (Todo)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39  | -(NSArray *)getEventsOfCalendar:(NSString *)calendarIdentifier {     EKCalendar *selectedCalendar = nil;     // There is function "calendarWithIdentifier" but the "Error getting shared calendar invitations for entity types 3" occured when you use. (nobody know the reason why)     // calendarWithIdentifier 함수가 있으나, "Error getting shared calendar invitations for entity types 3" 에러남. (왜 나는지 아무도 모름)     // so... we use if-clauses.     // 따라서.. 다 가져와서 비교한다.     NSArray *allCalendars = [self getSuitableEventCalendars];     for (EKCalendar *calendar in allCalendars) {         if ([calendar.calendarIdentifier isEqualToString:calendarIdentifier]) {             selectedCalendar = calendar;             break;         }     }     if (selectedCalendar == nil) {         return nil;     }     NSArray *calendarsArray = @[selectedCalendar];     // whenever you setted endDate, iOS will return 4years of events after startDate (nobody know the reason why)     // 캘린더 이벤트는 endDate가 언제든지 startDate로부터 4년 단위로밖에 나오지 않음. 모든 캘린더 이벤트를 가져오려면 loop를 돌아야 한다. (왜 그러는지 아무도 모름)     NSMutableSet *eventsSet = [NSMutableSet set];     NSDate *endDate =  [NSDate dateWithTimeIntervalSinceNow:[[NSDate distantFuture] timeIntervalSinceReferenceDate]]; // no end     for (NSTimeInterval startTime=0; startTime < endDate.timeIntervalSince1970; startTime += (60 * 60 * 24 * 365 * 4)) {         NSDate *startDate = [NSDate dateWithTimeIntervalSince1970:startTime];         NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendarsArray];         NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];         if (0 < events.count) {             [eventsSet addObjectsFromArray:events];         }     }     // 정렬해서 리턴     // return with sort.     return [[eventsSet allObjects] sortedArrayUsingSelector:@selector(compareStartDateWithEvent:)]; }  | cs | 
4. you can access iOS events finally !!
반응형
    
    
    
  'Programming > iOS' 카테고리의 다른 글
| [iOS/Objective-c] Touch Id 사용하여 인증하기. (passcode도 동작) (0) | 2015.11.25 | 
|---|---|
| [iOS/Objective-c]NSArray vs NSMutableArray 잘 사용하고 계신가요? (0) | 2015.11.19 | 
| iOS/Objective-c, code block 내에서 self 다루기 (2) | 2015.11.19 | 
| Objective-c Category에도 overriding이 가능?? (0) | 2015.11.07 | 
| NSSet, NSMutableSet 중복제거 (0) | 2015.11.07 | 
                          Comments