问题
I am attempting to create a local calendar on iOS. I request access to EKEntityTypeEvent
and have it granted, create the calendar from the EKEventStore
(yes, the same instance I requested access from), find the EKSourceTypeLocal
then set it on my new calendar. Calling saveCalendar:commit:error
(with commit:YES
) returns YES
and there is no NSError
. The resulting calendar has a calendarIdentifier
assigned.
But then when I flip to the iOS Calendar app, my calendar is not there! I've tried on the iOS 7 and 8 simulators (after a "Reset content and settings...", so there's no iCloud configured) and on an iCloud-connected iPhone 5s with iOS 8. Nothing works!
What have I missed?
I have created a bare project with the following view controller to illustrate the problem:
@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:
I've tested this code on a device, and it works OK (change EKEntityMaskEvent
-> EKEntityTypeEvent
). I can see new calendar in iOS calendars.
Looks like simulator does not actually saves your calendars. I've got the same issue some time ago.
来源:https://stackoverflow.com/questions/26163631/cant-create-local-calendar-on-ios