How to Create and Save Ekcalendar on iOS 6

How to create and save EKCalendar on ios 6

I found a solution. The problem is that when iCloud calendars switched on, it hides the locally created ones from the calendar app. To bypass this problem the solution is to add a new calendar to iCloud source:

    for (EKSource *source in self.eventStore.sources)
{
if (source.sourceType == EKSourceTypeCalDAV &&
[source.title isEqualToString:@"iCloud"]) //Couldn't find better way, if there is, then tell me too. :)
{
localSource = source;
break;
}
}

if (localSource == nil)
{
for (EKSource *source in self.eventStore.sources)
{
if (source.sourceType == EKSourceTypeLocal)
{
localSource = source;
break;
}
}
}

how do I create a new EKCalendar on iOS device?

This is how it is done on iOS 5 using the EventKit framework:

First of all you need an EKEventStore object to access everything:

EKEventStore *store = [[EKEventStore alloc] init];

Now you need to find the local calendar source, if you want the calendar to be stored locally. There are also sources for exchange accounts, CALDAV, MobileMe etc.:

// find local source
EKSource *localSource = nil;
for (EKSource *source in store.sources)
if (source.sourceType == EKSourceTypeLocal)
{
localSource = source;
break;
}

Now here's the part where you can fetch your previously created calendar. When the calendar is created (see below) there is an ID. This identifier has to be stored after creating the calendar so that your app can identify the calendar again. In this example I just stored the identifier in a constant:

NSString *identifier = @"E187D61E-D5B1-4A92-ADE0-6FC2B3AF424F";

Now, if you don't have an identifier yet, you need to create the calendar:

EKCalendar *cal;
if (identifier == nil)
{
cal = [EKCalendar calendarWithEventStore:store];
cal.title = @"Demo calendar";
cal.source = localSource;
[store saveCalendar:cal commit:YES error:nil];
NSLog(@"cal id = %@", cal.calendarIdentifier);
}

You can also configure properties like the calendar color etc. The important part is to store the identifier for later use.
On the other hand if you already have the identifier, you can just fetch the calendar:

else
{
cal = [store calendarWithIdentifier:identifier];
}

I put in some debug output as well:

NSLog(@"%@", cal);

Now you either way have a EKCalendar object for further use.

EDIT: As of iOS 6 calendarWithEventStore is depreciated, use:

cal = [EKCalendar calendarForEntityType:<#(EKEntityType)#> eventStore:<#(EKEventStore *)#>];

How to create and save EKCalendar on ios 6

I found a solution. The problem is that when iCloud calendars switched on, it hides the locally created ones from the calendar app. To bypass this problem the solution is to add a new calendar to iCloud source:

    for (EKSource *source in self.eventStore.sources)
{
if (source.sourceType == EKSourceTypeCalDAV &&
[source.title isEqualToString:@"iCloud"]) //Couldn't find better way, if there is, then tell me too. :)
{
localSource = source;
break;
}
}

if (localSource == nil)
{
for (EKSource *source in self.eventStore.sources)
{
if (source.sourceType == EKSourceTypeLocal)
{
localSource = source;
break;
}
}
}

Appropriate situations to create new EKCalendar iOS

There might be many reasons why you want to create a new calendar. Personally, I choose to create a new calendar when I want to separate a group of events from the ones that were created and tied to the default one. By this way, you also take advantage of bulk deleting of those newly created events when you think you don't need them. Just delete the calendar and all of its events will be purged as well.

By the way, if you are not sure what should be the source (iCloud, local, maybe tied to some mail account etc.) of the calendar you want to create, just use the source of default one:

let newCalendar = EKCalendar(forEntityType: .Event, eventStore: eventStore)
newCalendar.source = eventStore.defaultCalendarForNewEvents.source

Also make sure that the default calendar's allowsContentModifications property returns true if you want to use its source otherwise it is very likely that you won't be able to create an event within the new calendar.

Create a subscribed EKCalendar on iCloud?

No you cant create the subscribed calendar on iOS. Checkout the Notes in the support forum

Hope this helps.

iOS EKEvent Store recreating iCloud calendars in a loop, won't save local.

I'm not sure why you get this behavior, but I think due to the fact that having disabled iCloud, the system cannot make queries on it and then queue creation requests that are resolved once you wake iCloud (but I'm assuming).

Anyway, the first solution that comes to my mind is to check if iCloud is active or not in this way

EKSource *defaultSource = [eventStore defaultCalendarForNewEvents].source;

if (defaultSource.sourceType == EKSourceTypeCalDAV)
NSLog(@"iCloud Enable");
else
NSLog(@"iCloud Disable");

this is done you can save your events to the default source properly and then keep the 2 calendars (the local one and the cloud one) synchronized with each other ...

Reactivation of iCloud will still be prompted to add all local calendars.

see also the second answer here Accessing programmatically created calendar on iOS device (which is where I got the idea ;) )

I hope I was helpful.

EDIT: Maybe is not necessary to create a second calendar... Try to change the source of the calendar from EKSourceTypeCalDAV to EKSourceTypeLocal ... don't forget to save calendar with commit "YES"

EDIT2: Ok just tested ...

substitute this:

} else { // Create Calendar

EKSource *theSource = nil;

for (EKSource* src in eventStore.sources) {
if ([src.title isEqualToString:@"iCloud"]) {
theSource = src;
break;
}
if (src.sourceType == EKSourceTypeLocal && theSource==nil) {
theSource = src;
break;
}
}

[self setupCalendarWithSource:theSource withEvent:event];
}

with this ...

} else { // Create Calendar

EKSource *theSource = [eventStore defaultCalendarForNewEvents].source;

[self setupCalendarWithSource:theSource withEvent:event];
}

this way u will create the calendar in the right source (local if user deactivate iCloud and CalDAV otherwise)

then:

1) when user choose to deactivate iCloud should leave calendars on iphone (and not delete) so u have the cloud calendar in the local source

2) when user choose to activate iCloud will merge his local calendars with the cloud and there u go!!

i hope this help



Related Topics



Leave a reply



Submit