无法在iOS上创建本地日历

时间:2014-10-02 14:56:54

标签: ios eventkit

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

但是当我转到iOS日历应用程序时,我的日历不存在!我已尝试使用iOS 7和8模拟器(在#34;重置内容和设置......"之后,因此没有配置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

1 个答案:

答案 0 :(得分:1)

我已经在设备上测试了这段代码,但它运行正常(更改EKEntityMaskEvent - > EKEntityTypeEvent)。我可以在iOS日历中看到新的日历。 看起来模拟器实际上不会保存您的日历。我不久前有同样的问题。