iPhone EKEvent Availability, i try to set it, but it wont change - iphone

Im trying to set the availability of an event i import into the iphone calendar with my app. Im using this code:
[event1 setAvailability:EKEventAvailabilityFree];
When i sync my iphone to my iCal i check the availability of the event, and it says "Busy". Xcode do ask for an integer, but there is none for the free option. I get no errors when i run this code, can someone please help figure out whats wrong.

The problem is probably that the event's calendar hasn't been set. When the calendar is not set, the event doesn't know if it's valid to set availability (not all calendars support availability).
Try the following:
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = subject;
event.calendar = [eventStore defaultCalendarForNewEvents];
if (event.availability != EKEventAvailabilityNotSupported) {
event.availability = EKEventAvailabilityFree;
}

Further to David Hunt's answer it would appear that you need to set the calendar before you set the availability in your code. The sequence matters.
E.g.
event.calendar = //calendar object
event.availability = //availability typedef

Did you save the event back to the event store that contains it? See the saveEvent:span:error: message in the EKEventStore class reference.

Related

Using Calendar EventKit to fulfil a specific purpose for my app

Hey guys I am a beginner programmer making my first app. I am trying to create a "note to future self" IOS app on Xcode. Basically you add a note that you want to share with yourself at any particular date in the future. Does anyone have any suggestions as to how I can implement this either by using Eventkit or by some other method?
You can do it like this, here is the play by play:
You have to add NSCalendarsUsageDescription key in your plist file, because you are accessing user's privacy sensitive data
Go to your ViewController, import EventKit
Add this code on your viewDidLoad on any viewcontroller to add event to device calendar, you can move this code later based on how you gonna build your app
let eventStore = EKEventStore()
let event = EKEvent(eventStore: eventStore)
event.title = "My First Event"
event.startDate = NSDate() as Date
event.endDate = event.startDate.addingTimeInterval(60*60)
event.calendar = eventStore.defaultCalendarForNewEvents
do{
try eventStore.save(event, span: .thisEvent, commit: true)
}catch let err{
print("Error: \(err.localizedDescription)")
}
Here's what we do
we made an object from EKEventStore class, EKEventStore is an application’s point of contact for accessing calendar and reminder data. You can read more about it here EKEventStore
we made an event object, set its name as "My First Event", its startDate as current date, its end date as current time plus 2 hours, and eventually its calendar to default calendar set on user's device
then we try to insert the event object we just made to device calendar, and voila
remember to wrap save function in a do catch block since it could throw error
Welcome to SO, next time please provide more explanation about your question, could be what have you done or what have gone wrong doing it :D

Using Google Calendar API in Swift

I worked through the iOS Quickstart Guide provided by google to notice, that it is outdated for a very long time already.
So I researched the whole day to find out how it is supposed to work now but I did not find a working solution / description on how to do it.
I have an iOS App with a calendar. The user can decide which calendar should be used to synchronise calendar events between app and calendar. Google Calendar and Apple Calendar should be supported. The Apple Calendar synchronisation is working perfectly. For the Google Calendar I could not find any solution yet. It should be possible for users of an google calendar to synchronize all events with our iOS app (including adding, removing and changing events).
Is there any source for that which is not outdated and which is describing on how to do it?
Google has an example app on Github here: https://github.com/google/google-api-objectivec-client-for-rest
If you dig through the app, there's an Objective-C example of creating an event, it should give you a good place to start for Swift as well:
- (void)addEvent:(GTLRCalendar_Event *)event {
GTLRCalendarService *service = self.calendarService;
GTLRCalendar_CalendarListEntry *selectedCalendar = [self selectedCalendarListEntry];
NSString *calendarID = selectedCalendar.identifier;
GTLRCalendarQuery_EventsInsert *query =
[GTLRCalendarQuery_EventsInsert queryWithObject:event
calendarId:calendarID];
self.editEventTicket = [service executeQuery:query
completionHandler:^(GTLRServiceTicket *callbackTicket,
GTLRCalendar_Event *event,
NSError *callbackError) {
// Callback
self.editEventTicket = nil;
if (callbackError == nil) {
[self displayAlert:#"Event Added"
format:#"Added event \"%#\"",
event.summary];
[self fetchSelectedCalendar];
} else {
[self displayAlert:#"Add failed"
format:#"Event add failed: %#", callbackError];
}
}];
}
Additionally, you can also use the EventKit and let the user sync their calendar as they see fit, as described here: ios Add Event to google calendar

How to check if EKCalendar is visible

I run in the situation that at least local calendars of type EKCalendarTypeLocal are listed by EKEventStore but arent't shown and can not be selected in the calendar app on iOS.
if ([eventStore respondsToSelector:#selector(calendarsForEntityType:)]) cals = [eventStore calendarsForEntityType:EKEntityTypeEvent];
else cals = eventStore.calendars;
I found out that it has probably something to do with iCloud Accessing programmatically created calendar on iOS device.
I also created a local calendar successfully and could add events to it. This created calendar was not listed by EKEventStore but I get it with its identifier:
aCal = [eventStore calendarWithIdentifier:calId];
After I removed the iCloud account in the testing device all local calendars are shown also my own created. Is there any possibility to check if local calendars are hidden? So I can wrote an info text or hide them as well.
Have u solved?
Anyway, i'll try to answer.
As i understand iCloud don't let local calendars to be shown. If u turn of iCloud and then lead him back to on u will see a message like "Would u like to add your local calendar to iCloud?", so ... if the problem is just see the calendars in iCloud, extract your EKCalendar and change source in a EKSourceTypeCalDAV instead than EKSourceTypeLocal ... should be ok

EkEventStore Duplicate events

I m working iOS calendar events in my app.
I dont want to add duplicate event i.e if event is not modified i dont want to add it.
I m using saveEvent:event span:EKSpanThisEvent error:&error method from EkEventStore. As per documentation, this method returns NO if event is already in store.
But m getting duplicate events also in Calendar. Please suggest me something if m doing wrong anything.
Thanks.

Monotouch - add event to iphone calendar from my app

is it possible for my app to programatically add events (with permission from the user) to the iphones calendar?
cheers
w://
It's available in the iPhone 4 SDK, so in Monotouch. Take a look at EKEventStore and EKEvent. Here's the Apple docs.
Here's an example from a snippets page I keep:
public void CalendarEvents()
{
EKEventStore store = new EKEventStore();
EKCalendar calendar = store.DefaultCalendarForNewEvents;
// Query the event
if (calendar != null)
{
// Searches for every event in the next year
NSPredicate predicate = store.PredicateForEvents(NSDate.Now,DateTime.Now.AddDays(360),new EKCalendar[] {calendar});
store.EnumerateEvents(predicate, delegate(EKEvent currentEvent, ref bool stop)
{
// Perform your check for an event type
});
// Add a new event
EKEvent newEvent = EKEvent.FromStore(store);
newEvent.Title = "Lunch at McDonalds";
newEvent.Calendar = calendar;
newEvent.StartDate = DateTime.Now.Today;
newEvent.EndDate = DateTime.Now.Today.AddDays(1);
newEvent.Availability = EKEventAvailability.Free;
store.SaveEvent(newEvent, EKSpan.ThisEvent, new IntPtr());
}
}
A lateral thinking way around this is to consider creating an ICS file, then creating an email (which can be done programmatically) and then send it to the user or whomever they want. This is how I'm getting around this issue in my code. The beauty of it is it also doesn't create the sense in the user that your application is going to manage the dates, i.e. if they change something in your application that you'll cancel and rebook things on the fly.
It's not possible in the official iPhone API, so I doubt it's possible via mono touch.
Not directly possible with the SDK. You might try getting the user to subscribe to an online calendar published from your server, then upload events to that, but you'll probably run into all kinds of syncing issues.