我试图创建iOS上的本地日历。 我请求访问EKEntityTypeEvent
并将它授予创建从日历EKEventStore
(是的,相同的情况下,我要求从接入),找到EKSourceTypeLocal
然后将它放在我的新日历。 调用saveCalendar:commit:error
(与commit:YES
)返回YES
并没有NSError
。 得到的日历有一个calendarIdentifier
分配。
但后来当我翻到iOS日历应用程序,我的日历是不存在的! 我试过在iOS 7和8个模拟器(一个“重置内容和设置...”后,所以没有配置的iCloud)和一个的iCloud连接的iPhone 5S与iOS 8.没有什么工作!
有什么我错过了?
我已创建了以下视图控制器裸项目来说明这个问题:
@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