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.
Related
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
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
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
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.
I would like to know whether we can create a calendar using NSCalender and show it in our dedicated application. We can create a NSCalender object by
****NSCalendar *japaneseCalendar = [[NSCalendar alloc] initWithCalendarIdentifier : NSJapaneseCalendar];****
But how can we show this calender in out application. Is there any way to show the default calander in our application when a user click a button.
Please please guide me, i am hanging here for last few days.
Thank You
Rahul
NSCalendar has nothing to with the Calendar app on the iPhone, it's simply a class used to represent a calendar for whatever reason you need one.
I'm not sure that this can be done on the iPhone from within an app.
-
After re-reading your question, I think I misunderstood what you were asking.
If you want to display some kind of calendar view within the app using that particular calendar, you'll have to create you own view and draw the calendar into it.
You may be able to find some code on the internet to accomplish this already.