Reputation: 495
I had my EKEvent working fine and posting to Calendar.app perfectly in iOS4. Now all of a sudden it doesnt work in iOS5 or 6..?
I've searched the web for hours and i cant seem to find a solution?
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMddHHmm"];
NSLog(@"eventDateTime: %@", eventDateTime);
NSDate *date = [dateFormat dateFromString:@"201207202030"];
eventStore = [[EKEventStore alloc] init];
//defaultCalendar = [eventStore defaultCalendarForNewEvents];
venueEvent = [EKEvent eventWithEventStore:eventStore];
//venueEvent.calendar = Calendar;
venueEvent.title = event.name;
venueEvent.location = @"The Blvd Tavern";
venueEvent.startDate = date;
venueEvent.endDate = [[NSDate alloc] initWithTimeInterval:14400 sinceDate:venueEvent.startDate];
EKAlarm *eventAlarm1 = [EKAlarm alarmWithRelativeOffset:-28800];
NSArray *alarmArray = [[NSArray alloc] initWithObjects:eventAlarm1, nil];
venueEvent.alarms = alarmArray;
[venueEvent setCalendar:[eventStore defaultCalendarForNewEvents]];
//[venueEvent setCalendar:defaultCalendar];
NSError *err = [[NSError alloc] init];
[eventStore saveEvent:venueEvent span:EKSpanThisEvent error:&err];
NSLog(@"%@", err);
I keep gettin this error log when i run this code
Error Domain=EKErrorDomain Code=1 "No calendar has been set." UserInfo=0x1cd2a350 {NSLocalizedDescription=No calendar has been set.}
Upvotes: 2
Views: 1212
Reputation: 149
float version = [[UIDevice currentDevice].systemVersion floatValue];
if(version >= 6.0){
EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
if(status == EKAuthorizationStatusNotDetermined){
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error){
if(!granted){
Alert!
}
}];
}else if(status == EKAuthorizationStatusDenied){
Alert!
}
}
Upvotes: 2
Reputation: 111
You need to ask the user for permission to use the calendar before you can modify it. Try this:
if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
// do stuff here
}
}
Source: https://jeboyer.wordpress.com/2012/09/26/handling-access-to-calendars-in-ios-6-2/
Upvotes: 4