2013-10-04 70 views
6
#import <EventKit/EventKit.h> 

我无法在默认iOS日历中创建活动。无法在默认iOS 7日历中创建活动

EKEventStore *eventStore = [[EKEventStore alloc] init]; 
for (EKSource *source in eventStore.sources) 
{ 
    if (source.sourceType == EKSourceTypeCalDAV || source.sourceType == EKSourceTypeLocal) 
    { 
     NSLog(@"I found it"); 
     break; 
    } 
} 

从这里开始它不能返回任何来源。当我构建并运行应用程序时,没有任何请求可以访问默认日历。

所有的一切我得到一个空数组:

[eventStore.sources count] 

即使我想,而无需创建新的日历添加事件(使用

[eventStore defaultCalendarForNewEvents] 
+0

你有事件许可吗? –

回答

7

我想有一个问题在访问EKEventStore,以检查权限尝试以下,

EKEventStore *eventStore = [[EKEventStore alloc] init]; 
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]){  
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) 
     { 
      NSLog(@"GRANTED: %c", granted); 
      for (EKSource *source in eventStore.sources) 
      { 
       if (source.sourceType == EKSourceTypeCalDAV || source.sourceType == EKSourceTypeLocal) 
       { 
        NSLog(@"I found it"); 
        break; 
       } 
      } 
     }]; 

} 

希望它有助于你..

+1

你不应该检查系统版本,但是否有方法可用:'if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]){' – rckoenes

+0

@rckoenes:你是对的,谢谢,我已经更新答案。 –

2

我发现问题:

[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { 
    // TODO 
}]; 

我必须手动申请许可,想到权限集没有它,我认为它已经固定在iOS的7.0.2版本。