How to detect google calendar in local EventStore - ios

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.

Related

How to Create events using Google Calendar API iOS Swift

I want to develop one demo app which will create events using my Application, store it using Google Calendar API and then fetch all the data and gives reminder. I have referred this link to fetch data and for setup, but I am not getting how to create events. Can anyone guide me how can I do this? I searched a lot for creating events using iOS, but I don't get anything useful for Google Calendar API, I found all the stuff using EventKit framework.
Please help me.
Thank You.
I've found a tutorial: "iOS SDK: Working with Google Calendars", in this tutorial they provide insights on downloading the calendars, and creating a new event with a description and a date/time.
Regarding our app structure, the basic view is going to be a table view that will contain three sections for setting the following data:
An event description
An event date/time
A target calendar
A code example for adding event(Objective C):
// Create the URL string of API needed to quick-add the event into the Google calendar.
// Note that we specify the id of the selected calendar.
NSString *apiURLString = [NSString stringWithFormat:#"https://www.googleapis.com/calendar/v3/calendars/%#/events/quickAdd",
[_dictCurrentCalendar objectForKey:#"id"]];
// Build the event text string, composed by the event description and the date (and time) that should happen.
// Break the selected date into its components.
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit
fromDate:_dtEvent];
if (_isFullDayEvent) {
// If a full-day event was selected (meaning without specific time), then add at the end of the string just the date.
_strEventTextToPost = [NSString stringWithFormat:#"%# %d/%d/%d", _strEvent, [dateComponents month], [dateComponents day], [dateComponents year]];
}
else{
// Otherwise, append both the date and the time that the event should happen.
_strEventTextToPost = [NSString stringWithFormat:#"%# %d/%d/%d at %d.%d", _strEvent, [dateComponents month], [dateComponents day], [dateComponents year], [dateComponents hour], [dateComponents minute]];
}
// Show the activity indicator view.
[self showOrHideActivityIndicatorView];
// Call the API and post the event on the selected Google calendar.
// Visit https://developers.google.com/google-apps/calendar/v3/reference/events/quickAdd for more information about the quick-add event API call.
[_googleOAuth callAPI:apiURLString
withHttpMethod:httpMethod_POST
postParameterNames:[NSArray arrayWithObjects:#"calendarId", #"text", nil]
postParameterValues:[NSArray arrayWithObjects:[_dictCurrentCalendar objectForKey:#"id"], _strEventTextToPost, nil]];
I think you can implement this from what you have started in the iOS Quickstart Google.
I hope this helps. Goodluck :)
first you have to create a public API and get is key. and set its url using the following code
let url = NSURL(string: "https://www.googleapis.com/calendar/v3/calendars/email.gmail.com/events?maxResults=15&key=APIKey-here")
It works for you

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

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

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)

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