How to Create events using Google Calendar API iOS Swift - ios

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

Related

Implementing persistent countdown timer

There are some games where certain actions are time related. For example, every 24 hours user gets one credit if he played a game ... After we turn off our phone and come back after few hours timer is correctly set.
How is this done ? There must be some method which writes current time into a persistent storage and then reads from it?
I am just guessing it's something like this:
NSDate * now = [NSDate date];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"HH:mm:ss"];
NSString *newDateString = [outputFormatter stringFromDate:now];
And then comes part where newDateString is written into persistent storage.
Also, if this is a method, what happens if user change time manually on his device? Can we rely on this or there are some other methods to track real time passed between certain moments in the game?
You could do this by writing the next award time to NSUserDefaults and comparing previous values.
// Fetch previous "next award" time.
NSNumber *previousTime = [[NSUserDefaults standardUserDefaults] objectForKey:#"next_award_time"];
NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
// If we haven't yet scheduled a "next award" time, or if we've passed it..
if (!previousTime || [previousTime doubleValue] < now) {
// We had a previous value, so we've passed the time.
if (previousTime) {
// Award user.
}
// Set next award time.
NSTimeInterval nextTime = [[NSDate date] timeIntervalSince1970] + 60*60*24;
[[NSUserDefaults standardUserDefaults] setObject:#(nextTime) forKey:#"next_award_time"];
}
As far as worrying about users changing their own device time, you'll need to make a network request to an unbiased third-party clock. This could be a server you control or a well-known time server. You might want to check out this answer for help on how to contact an NTP.

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.

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)

How to disable (user interaction OFF) past dates in the tapku library calendar in iOS

I am using tapku calendar library in iOS. Works perfectly fine! I want to disable (user interaction disabled) all the previous dates from the current date.
So, the user should be able to click the current date and future dates only.
I looked over all places but can't find a solution.
Any help would be appreciated.
Thank you
There will be tile tapped method in tapkucalendar view class. You need to check selected KLDate is later or ealrier depending upon your requirement and can block by returning nil param.
Check the method tile when selected and do changes. I have followed and blocked for future dates and also dates beyond a year.
Instead of Disabling past dates here I am throwing an alert like 'Invalid date' using below code.
- (void)calendarMonthView:(TKCalendarMonthView *)monthView didSelectDate:(NSDate *)selectedDate
{
if([selectedDate compare:[NSDate date]] == NSOrderedAscending)
{
NSString *today=[NSString stringWithFormat:#"%#",[NSDate date]];
NSString *chooseday=[NSString stringWithFormat:#"%#",selectedDate];
NSArray *date1=[today componentsSeparatedByString:#" "];
NSArray *date2=[chooseday componentsSeparatedByString:#" "];
if([[date1 objectAtIndex:0] isEqualToString:[date2 objectAtIndex:0]])
{
NSLog(#"Today date clicked");
}
else
{
NSLog(#"Past date clicked");
}
}
}

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];

Resources