Using Calendar EventKit to fulfil a specific purpose for my app - swift

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

Related

Using NSLocalNotification for multiple reminders

I am creating a reminder based app and my only problem right now is being able to implement reminders on my app.
Basically the user is presented with a UITableView where the user can add events and I'd like to fire these dates with a notification to the user reminding them of the event on a date saved to Core Data.
At this time, I still do not understand how NSLocalNotification works an I have heard some users here say how apple only allows 64 notifications to be processed at a time and I only managed to create 1 but when creating another event, It is down to 0 and randomly goes back to 1 event when I debug my app with the
UIApplication.sharedApplication().scheduledLocalNotifications?
and I can see the notification I set but not the other ones.
Any sample app in swift would greatly help me understand how I can set multiple notifications to add, edit, delete them.
Thanks in advance and happy coding
The easiest way to tackle this is to just create notifications and schedule them. If you want to remove specific notifications you will have to hold on to their references and persist them across app launches, however you can always remove all the currently scheduled notifications with UIApplication.sharedApplication().cancelAllLocalNotifications()
This is an example of scheduling a notification to fire once a day at the same time for 5 days:
let secondsInADay = 60 * 60 * 24
for i in 1...5 {
var dayString = "\(i) days"
if i == 1 {
dayString = "\(i) day"
}
let notification = UILocalNotification()
notification.fireDate = NSDate(timeInterval: Double(i * secondsInADay), sinceDate: NSDate())
notification.alertBody = "It has been \(dayString) since you last opened the app."
notification.soundName = UILocalNotificationDefaultSoundName
if notification.fireDate?.timeIntervalSinceNow > 0 {
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
}
Also, you are correct, each app is given a queue for local notifications that have yet to fire. That queue can hold 64 individual notifications (a repeating notification counts as only 1 of those 64).

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

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

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.

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.

How To Call/Create iPhone Calendar In A User Application

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.