2014-10-02 4 views
1

iOS에서 로컬 캘린더를 만들려고합니다. EKEntityTypeEvent에 대한 액세스를 요청하고 EKEventStore (예, 내가 액세스 권한을 요청한 동일한 인스턴스)에서 캘린더를 만들고 EKSourceTypeLocal을 찾은 다음 새 캘린더에 설정하십시오. saveCalendar:commit:error (commit:YES)을 호출하면 YES이 반환되고 NSError은 반환되지 않습니다. 결과 달력에는 calendarIdentifier이 할당됩니다.iOS에서 로컬 캘린더를 만들 수 없음

하지만 iOS 캘린더 앱으로 전환하면 내 캘린더가 표시되지 않습니다. iOS 7 및 8 시뮬레이터 ("Reset Content and Settings ..."이후, iCloud가 구성되지 않은 경우) 및 iOS 8이 설치된 iCloud 연결 iPhone 5s에서 시도했습니다. 아무 것도 작동하지 않습니다!

내가 무엇을 놓쳤는가?

@import EventKit; 
#import "ViewController.h" 

@interface ViewController() 

@property (weak, nonatomic) IBOutlet UILabel *resultLabel; 

@property (nonatomic, assign) NSUInteger calendarCount; 
@property (nonatomic, strong) EKEventStore *eventStore; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    self.resultLabel.text = @"";   
    self.eventStore = [[EKEventStore alloc] init]; 
} 

- (IBAction)userDidTapCreateCalendar:(id)sender { 
    EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent]; 

    if (status == EKAuthorizationStatusNotDetermined) { 
     __weak typeof(self) weakSelf = self; 
     [self.eventStore requestAccessToEntityType:EKEntityTypeEvent 
             completion:^(BOOL granted, NSError *error) { 
              if (granted) { 
               [weakSelf createCalendar]; 
              } else { 
               weakSelf.resultLabel.text = @"If you don't grant me access, I've got no hope!"; 
              } 
             }]; 
    } else if (status == EKAuthorizationStatusAuthorized) { 
     [self createCalendar]; 
    } else { 
     self.resultLabel.text = @"Access denied previously, go fix it in Settings."; 
    } 
} 

- (void)createCalendar 
{ 
    EKCalendar *calendar = [EKCalendar calendarForEntityType:EKEntityMaskEvent eventStore:self.eventStore]; 
    calendar.title = [NSString stringWithFormat:@"Calendar %0lu", (unsigned long)++self.calendarCount]; 

    [self.eventStore.sources enumerateObjectsUsingBlock:^(EKSource *source, NSUInteger idx, BOOL *stop) { 
     if (source.sourceType == EKSourceTypeLocal) { 
      calendar.source = source; 
      *stop = YES; 
     } 
    }]; 

    NSError *error = nil; 
    BOOL success = [self.eventStore saveCalendar:calendar commit:YES error:&error]; 
    if (success && error == nil) { 
     self.resultLabel.text = [NSString stringWithFormat:@"Created \"Calendar %0lu\" with id %@", 
           (unsigned long)self.calendarCount, calendar.calendarIdentifier]; 
    } else { 
     self.resultLabel.text = [NSString stringWithFormat:@"Error: %@", error]; 
    } 
} 

@end 

답변

1

내가 장치에서이 코드를 테스트했습니다, 그리고 확인 (변경 EKEntityMaskEvent ->EKEntityTypeEvent)를 작동합니다

나는 문제를 설명하기 위해 다음과 같은 뷰 컨트롤러와 함께 베어 프로젝트를 만들었습니다. iOS 캘린더에서 새 캘린더를 볼 수 있습니다. 시뮬레이터가 실제로 캘린더를 저장하지 않는 것처럼 보입니다. 얼마 전에 같은 문제가 발생했습니다.

+0

물론 그렇습니다. 이것은 심지어 이것이 컴파일된다는 믿음을 사로 잡습니다. _ 역동적 인 언어가 너무 멋지다 _ _ –