2014-10-02 63 views
1

我正在尝试在iOS上创建本地日历。我请求访问EKEntityTypeEvent并授予它,从EKEventStore(是的,我请求访问的同一个实例)创建日历,找到EKSourceTypeLocal,然后将其设置在我的新日历上。致电saveCalendar:commit:error(与commit:YES)返回YES并且没有NSError。生成的日历分配有calendarIdentifier无法在iOS上创建本地日历

但是当我翻转到iOS日历应用程序时,我的日历不在那里!我尝试过iOS 7和8模拟器(在“重置内容和设置...”之后,因此没有配置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

当然就是这样。它乞丐认为,这甚至编译。 _但动态语言太棒了! – 2014-10-03 08:19:22