iOS - Can't add calendar event to Gmail calendar with EventKit - ios

I want to add a new calendar event to the default iOS calendar. That works fine, until a user uses Gmail for syncing calendars.
I use the following code:
if (!calendar) {
calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore];
// set calendar name
[calendar setTitle:#"My calendar"];
EKSource *theSource = [eventStore defaultCalendarForNewEvents].source;
calendar.source = theSource;
// save this in NSUserDefaults data for retrieval later
NSString *calendarIdentifier = [calendar calendarIdentifier];
NSError *error = nil;
BOOL saved = [eventStore saveCalendar:calendar commit:YES error:&error];
if (saved) {
// saved successfuly, store it's identifier in NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:calendarIdentifier forKey:#"railplanner_calendar_identifier"];
} else {
// unable to save calendar
return NO;
}
}
This works fine when iCloud is enabled, or with local calendars.
But when a user uses Gmail for syncing calendars, my custom calendar doesn't appear in the calendars list. The local calendars disappear too.
Does anyone know how I can add a new calendar with a new event to a Gmail (or Google) Calendar?
Many thanks in advance.

This is likely to be impossible with the current Google Calendar synchronization, you could detect the type of the default calendar, and in the case of Google use the related Google iPhone API to create your new calendar (not really helpful I know)

Related

iOS unable to update calendar event when synced to exchange account

i'm having an issue updating a calendar event on my ipad with an exchange email account on it. it'll create new events no problem, but on an update, it'll create another one, instead of updating the existing. a few things
code below works for any other type of email account (say gmail)
code below works if i don't have an email (so it just writes to the local calendar)
yes i've checked that my exchange email has calendars enabled
the error being thrown is:
Error getting event with identifier 9E678016-F8E4-46B1-9043-E54E09A148F0:5A89FFAF15B1408386A9CBD518BBDD770: Error Domain=EKCADErrorDomain Code=1010
here is the code that i'm using.
[self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
dispatch_sync(dispatch_get_main_queue(), ^{
NSString *appleEventId = [item valueForKey:#"appleEventId"];
EKEvent *currentEvent = [self.eventStore eventWithIdentifier:[item valueForKey:#"appleEventId"]];
if (currentEvent){
NSLog(#"LOG101: found an event with %#",appleEventId );
[self createEvent:currentEvent inEventStore:self.eventStore forActivity:item];
}
else{
NSLog(#"LOG101: COULD NOT FIND an event with %#",appleEventId );
EKEvent *event = [EKEvent eventWithEventStore:self.eventStore];
[self createEvent:event inEventStore:self.eventStore forActivity:item];
}
completionHandler(YES);
});
}
else{
dispatch_sync(dispatch_get_main_queue(), ^{
completionHandler(NO);
});
}
}];
(create event does a bunch of customer logic, but ultimately calls the following to create an event)
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
it's frustrating because the code works for everything else but exchange. any tips / suggestions would be great. thanks
I've been pulling hairs for two days over a similar problem!
The documentation for eventIdentifier states:
"If the calendar of an event changes, its identifier most likely changes as well."
https://developer.apple.com/reference/eventkit/ekevent/1507437-eventidentifier?language=objc
Also the documentation for calendarIdentifier states:
"A full sync with the calendar will lose this identifier. You should have a plan for dealing with a calendar whose identifier is no longer fetch-able by caching its other properties."
https://developer.apple.com/reference/eventkit/ekcalendar/1507380-calendaridentifier?language=objc
I thinks it's safe to say you can't rely on saving calendar or event identifiers locally for later use if the calendar is synchronised :-(

Can't retrieve event store calendars?

I have the iCloud calendars and some subscribed calendars on my device, all calendars apps including the native calendar app display the calendars correctly, but in my app i can't retrieve them.
I am trying to retrieve the event store calendars as follow:
A)
NSArray *calendarArray = [self.eventStore calendarsForEntityType:EKEntityTypeEvent];
// always return result an empty array
B)
NSMutableArray *calendars = [[NSMutableArray alloc]init];
for (EKSource *source in self.eventStore.sources) {
NSSet *sourceCalendars = [source calendarsForEntityType:EKEntityTypeEvent];
[calendars addObjectsFromArray:sourceCalendars.allObjects];
}
// always return result an empty array
The event store instance is allocated and initialized correctly, and the privacy for accessing calendars also granted.
What can I do ?
Regards
When you have more than 100 calendar and Reminder lists then event store stop working properly.
For more info check this link: http://support.apple.com/kb/HT4489?viewlocale=en_US

How to detect google calendar in local EventStore

In setting of iPhone, I was login the google calendar.
I program an iPhone app, that can detect the event that is belonged to the logged in google calendar as above.
EKEventStore* eventStore;
eventStore = [[EKEventStore alloc] init];
NSPredicate* predicate = [eventStore predicateForEventsWithStartDate:firstDate endDate:lastDate calendars:eventStore.calendars];
NSArray* fetchedEvents = [eventStore eventsMatchingPredicate:predicate];
for(EKEvent* ecEvent in fetchedEvents)
{
// How to do
}
How can I do that?
I also research but almost have to:
Method 1: program for user choose an calender (include google calendar)
Method 2: program for get default calendar (may be that is google calendar)
Method 3: program a app that login and get google calendar as same as Using Google Calendar Api in iPhone have other method.
Thank you for your help.
Re: question 1, this will tell you if the event belongs to a google calendar:
EKCalendar *calendar = event.calendar;
if ([[calendar source] sourceType] == EKSourceTypeCalDAV &&
[[[calendar source] title] isEqualToString:#"Gmail"])
NSLog(#"found a Gcal event");
Sometimes (if there are multiple google accounts configured) [[calendar source] title] will return the gmail address instead of #"Gmail", so you may want to check for that as well.

Create new Reminders list with EventKit?

My app uses EventKit to read and write new reminders to and from the Reminders app, which works well. However, I've only found a way to write reminders to the default list that the user selects in the Settings app... My question is, does anyone know if there's a way to create a whole new list, rather than use the default list.
Nope.
Apple doesn't allow apps to write to lists other than the default list - looking through the docs yields no way to do so.
YES!!!
Looking through some more literature, I found this!
It seems that the EKReminder objects can be added to any list - based on my limited understanding, this should work at least to write to a different list:
NSArray *calendars = [_eventStore
calendarsForEntityType:EKEntityTypeReminder];
for (EKCalendar *calendar in calendars)
{
NSLog(#"Calendar = %#", calendar.title);
}
EKCalendar *calendar = ... //pick one.
EKReminder *reminder = [EKReminder reminderWithEventStore:self.eventStore];
reminder.title = #"Go to the store and buy milk";
reminder.calendar = calendar;
NSError *error = nil;
[_eventStore saveReminder:reminder commit:YES error:&error];

Create a Calendar (in the ios native calendars) through an Application on the iphone

I want to know if it is possible to create a new calendar for your iphone through an application. I know you can set up event with an app, but can I make another calendar through the app to place all the events too.
EDIT:
IF it is not possible what would be the best work around, make a calendar and event store, and save that information with nscoding? Then retrieve afterwards?
If there is a possibilty to create a new calendar to integrate with the existing calendars, that is what I want to do.
This is how I have done this:
-(NSString*)createCal:(NSString*)myCalId;{
// Instantiate eventstore object
EKEventStore *store = [[EKEventStore alloc] init];
EKSource *localSource = nil;
for (EKSource *source in store.sources)
if (source.sourceType == EKSourceTypeLocal){
localSource = source;
break;
}
//this is you creating your calendar
EKCalendar *cal;
cal = [EKCalendar calendarWithEventStore:store];
cal.title = #"Name of calendar";
cal.source = localSource;
[store saveCalendar:cal commit:YES error:nil];
NSLog(#"cal id = %#", cal.calendarIdentifier);
return cal.calendarIdentifier;}
You will need to import <EventKit/EventKit.h>
This is powerful so you need to be careful and do a lot of verifications for example if the calendar already exist, and so on. Hope this helps.
Looking at the documentation for EventKit, it doesn't seem possible to create new calendars programatically. However, you can get a list of existing calendars and find out the default calendar.
Hope this helps.
You can't use the standard init-Method anymore; it's deprecated. Here the quote from the EKEventStore Class Reference:
"On iOS 5 and later, initialize an event store object with the default init method. On iOS 6 and later, you must request access to an entity type after the event store is initialized with requestAccessToEntityType:completion: for data to return.
On OS X, use initWithAccessToEntityTypes: instead of the default init method. Acceptable entity types are EKEntityMaskEvent for events and EKEntityMaskReminder for reminders."
So if you use iOS6, it's somethin like:
EKEventStore *store = [[EKEventStore alloc] initWithAccessToEntityTypes:completion:];
and
cal = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:store];
I need it for my MacOSX App. So i have to use this:
So in my Case (MacOSX) it's this solution. It's Sparqs code; just modify with the new init instead of the the standard one:
-(NSString*)createCal:(NSString*)myCalId;{
// Instantiate eventstore object
EKEventStore *store = [[EKEventStore alloc] initWithAccessToEntityTypes:EKEntityMaskEvent];
EKSource *localSource = nil;
for (EKSource *source in store.sources)
if (source.sourceType == EKSourceTypeLocal){
localSource = source;
break;
}
//this is you creating your calendar
EKCalendar *cal;
cal = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:store];
cal.title = #"Name of calendar";
cal.source = localSource;
[store saveCalendar:cal commit:YES error:nil];
NSLog(#"cal id = %#", cal.calendarIdentifier);
return cal.calendarIdentifier;}

Resources