Events add all calendars in iOS 6.1? - iphone

I tried to get calendar title from my iPhone (iOS 6.1), but nothing found inside.
How to get calendar title from iOS 6.1? please help me.
Thanks in Adnvance
I tried this:
EKEventStore *store = [[EKEventStore alloc]init];
EKEvent *event = [EKEvent eventWithEventStore:store];
NSArray *list = [[NSArray alloc]init];
list = [[store calendarsForEntityType:EKEntityTypeEvent] copy];
// Calendars deprecate in iOs 6.1
// Retain deprecate, so use "copy"
NSLog(#"list: %d",[list count]);
for(EKCalendar *calendar in list)
{
NSLog(#"list: %#",calendar.title);
}

hope this helps....
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
}];
NSArray *arrCalenders = [store calendars];//below iOS 6.o
NSLog(#"%#", arrCalenders);
NSArray *arrayCalndars = [store calendarsForEntityType:EKEntityTypeEvent]; // In ios 6.o
NSLog(#"%#", arrayCalndars);
This displays the Current list of calendars...

Related

How to delete event from iCal added from my app.?

I'm developing an app in which when user add any event as his favorite, I'm adding that event to iCal but how do i remove that event from iCal if user removes that particular event from his favorite.?
Here is my code:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[self.parentDetailArray valueForKey:#"start_time_num"] intValue]];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
[dateformatter setLocale:[NSLocale currentLocale]];
[dateformatter setDateFormat:#"dd-MM-yyyy"];
NSString *dateString=[dateformatter stringFromDate:date];
EKEventStore *eventStore = [[EKEventStore alloc] init];
if([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)])
{
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted){
//---- codes here when user allow your app to access theirs' calendar.
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = title;
event.startDate = date;
event.endDate = [[NSDate alloc] initWithTimeInterval:1000 sinceDate:event.startDate];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
// Here I'm trying to get the identifier of that event but m getting only (null)
self.str = [[NSString alloc] initWithFormat:#"%#", event.eventIdentifier];
[self.arrayofEventId addObject:self.str];
//[self performCalendarActivity:eventStore];
}else
{
//----- codes here when user NOT allow your app to access the calendar.
}
}];
}
else {
//---- codes here for IOS < 6.0.
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"Testing for calendar";
event.startDate = [[NSDate alloc] init];
NSLog(#"%#",event.startDate);
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
//[self performCalendarActivity:eventStore];
}
How do i get the event identifier n remove that particular event from iCal. any help would be appreciated.
You could write all app generated event ID's to file and then load them back up next time the app loads. This way you can keep track of which events your app created. Then you may want to try this method:
- (BOOL)removeEvent:(EKEvent *)event span:(EKSpan)span commit:(BOOL)commit error:(NSError **)error
Save with this:
NSString *id = [[NSString alloc] initWithFormat:#"%#", event.eventIdentifier];
Remove with this:
EKEvent *event = [eventStore eventWithIdentifier:id];
NSError *error = nil;
[eventStore removeEvent:event span:EKSpanThisEvent error:&error];
Apple has documentation on the EKEventStore here.

Add Event to calendar in xcode iOS

Hy
I have this code for adding Events to calendar but it does not add.
-(void)event
{
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"Event";
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:#"dd.MM.yyyy HH:mm"];
NSString *dateandtime =[NSString stringWithFormat:#"%#%#%#",datestring,#" ",starttimestring];
NSString *dateandtimeend =[NSString stringWithFormat:#"%#%#%#",datestring,#" ",endtimestring];
event.startDate = [tempFormatter dateFromString:dateandtime];
event.endDate = [tempFormatter dateFromString:dateandtimeend];
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -60.0f * 24]];
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -15.0f]];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
}
From the XML I get the date and time in this format:
datestring: 28.10.2012
starttimestring: 15:00
Are you on the iOS 6 simulator or on a device with iOS 6? If so, you need to ask the user for permission to use the event store before you can save items to it.
Basically, if the requestAccessToEntityType:completion: selector is available on your event store object, you call that method and provide a block of code that is executed when the user grants permission, and you would then do your event saving in that block.
First add the EventKit framework to your project and don't forget to include the import:
#import <EventKit/EventKit.h>
Here is a code snippet that I used that worked for me:
EKEventStore *eventStore = [[EKEventStore alloc] init];
if ([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)])
{
// the selector is available, so we must be on iOS 6 or newer
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error)
{
// display error message here
}
else if (!granted)
{
// display access denied error message here
}
else
{
// access granted
// ***** do the important stuff here *****
}
});
}];
}
else
{
// this code runs in iOS 4 or iOS 5
// ***** do the important stuff here *****
}
[eventStore release];
Here is a blog post that I did on this subject:
http://www.dosomethinghere.com/2012/10/08/ios-6-calendar-and-address-book-issues/
1) add Eventkit framework and #import <EventKit/EventKit.h>
2)
-(void)syncWithCalendar {
EKEventStore *store = [EKEventStore new];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) { return; }
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = #"Event Title Testing"; //give event title you want
event.startDate = [NSDate date];
event.endDate = [event.startDate dateByAddingTimeInterval:60*60];
event.calendar = [store defaultCalendarForNewEvents];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
}];
}
3) call function
[self syncWithCalendar];

How to import an iPhone Calendar event to my app?

I am looking for some tutorials/guidelines for importing iPhone calendar event(s) to my app.
Can anyone help?
Thanks
-(void)EventList{
EKEventStore *eventStore = [[EKEventStore alloc] init];
if([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)]) {
// iOS 6 and later
ReadCalendar = TRUE;
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted){
//---- codes here when user allow your app to access theirs' calendar.
[self performCalendarActivity];
}else
{
//----- codes here when user NOT allow your app to access the calendar.
}
}];
}
else {
//---- codes here for IOS < 6.0.
[self performCalendarActivity];
}
}
-(void)performCalendarActivity{
self.eventStore = [[EKEventStore alloc] init];
eventsList = [[NSMutableArray alloc] initWithArray:0];
// Get the default calendar from store.
self.defaultCalendar = [self.eventStore defaultCalendarForNewEvents];
eventsList= [[self fetchEventsForToday] mutableCopy];
}
-(NSArray *)fetchEventsForToday {
NSDate *startDate;
NSDate *endDate;
startDate = [NSDate date];
endDate = [NSDate dateWithTimeIntervalSinceNow:86400];
// Create the predicate. Pass it the default calendar.
NSArray *calendarArray = [NSArray arrayWithObject:defaultCalendar];
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate
calendars:calendarArray];
// Fetch all events that match the predicate.
NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];
return events;
}

Find iOS Calendars

I have an app that creates a calendar event programmatically it is working and I just made it so it retrieves the users calendars and displays them in a picker but I have a memory leak. Can you see it becasue I tried releasing everything... In addition my main issue is how do I have it save to that calendar that the user selects, [event setCalendar:calendararray]; doesn't work. The calendararray is a EKCalendar *calendararray and I am setting the user selected calendar to it. Why does this not work???? How do I make it work...
calendar .m
#import "calendar.h"
#implementation calendar
#synthesize delegate;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
EKEventStore *eventStore = [[EKEventStore alloc] init];
/* These are the calendar types an iOS Device can have. Please note
that the "type" property of an object of type EKCalendar
is of type EKCalendarType. The values in the "CalendarTypes"
array reflect the exact same values in the EKCalendarType
enumeration, but as NSString values */
NSArray *calendarTypes = [NSArray arrayWithObjects:
#"Local",
#"CalDAV",
#"Exchange",
#"Subscription",
#"Birthday",
nil];
/* Go through the calendars one by one */
NSUInteger counter = 1;
for (EKCalendar *thisCalendar in eventStore.calendars){
/* The title of the calendar */
NSLog(#"Calendar %lu Title = %#",
(unsigned long)counter, thisCalendar.title);
EKEventStore has a calendars property which is an NSArray of EKCalendar instances. You can then get each EKCalendar's title and display all of them to the user so they can select the calendar they wish to add the new event to.
NSLog(#"Calendar %lu Title = %#", (unsigned long)counter, thisCalendar.title);
NSString *title = [NSString stringWithFormat:#"Calendar %lu Title = %#", (unsigned long)counter, thisCalendar.title];
NSLog(title);
I'm not sure if all the code you posted is from the same version, but if you threw that together I'd see at least the following problem:
You say
[event setCalendar:calendararray];
doesn't work.
It seems calendararray ist set in eventview.m's calendararray: method.
This in turn is called from calendar.m's pickerView:didSelectRow:, where it takes the selected object from arrayColors.
In calendar.m's viewDidLoad method, arrayColors is initialised with the users's calendars' titles, not the calendars.
So you end up giving EKEvent's setCalendar: method an NSString instead of an EKCalendar.
Not knowing the rest of your program, as a fix I would try to keep the EKCalendar objects themselves in arrayColors.
So, in calendar.m try to change the
[arrayColors addObject:thisCalendar.title];
in viewDidLoad to
[arrayColors addObject:thisCalendar];
Then change pickerView:titleForRow: to
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
EKEvent *event = [arrayColors objectAtIndex:row]
return event.title;
}
FInally, in eventview.m change calendararray:
- (void) calendararray:(EKCalendar *)array{
NSLog(#"calendarNameTextFieldStringFromTable %#", array);
calendararray = array;
calendarLabel.text = array.title;
calendarLabel.textColor = [UIColor brownColor];
}
Try this way:
#import "EventTestViewController.h"
#import "EventKit/EventKit.h"
#implementation EventTestViewController
- (void)viewDidLoad {
[super viewDidLoad];
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"EVENT TITLE";
event.startDate = [[NSDate alloc] init];
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
}
Ok i see what you want, heres my way:
EKEventStore *eventStore = [[EKEventStore alloc] init];
NSArray *calendarArray = [eventStore calendars]; //Here you get all the device calendars
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"Event Title";
event.startDate = [[NSDate alloc] init]; event.endDate = [[NSDate alloc] initWithTimeInterval:816 sinceDate:event.startDate];
[event setCalendar:[calendarArray objectAtIndex:1]]; //here you set which calendar you want..
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
You can NSLog with a FOR loop iterating "index" using this code below, to know which one you want.
NSLog(#"Cal name> %#. Index of cal> %i", [[calendarArray objectAtIndex:index] title], index);
Hope this helps

Removing events from iPhone calendar with EKEventStore

I'm trying to remove events that i have created from the iPhone calendar.
I tried this, but it always returns NO:
[eventStore removeEvent:event span:EKSpanThisEvent error:&err];
I created the event as follows and it works:
eventStore = [[EKEventStore alloc] init];
event = [EKEvent eventWithEventStore:eventStore];
event.title = #"EVENT TITLE";
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd:HH:mm"];
NSDate * date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:#"2010-8-15:12:30"];
[date retain];
event.startDate = date;
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
Is there a way to remove this event? Or it would be be better that if I try to write this event again it only modifies it instead of creating a new one.
Thanks,
After creating the event I save the eventIdentifier in an array:
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSString* str = [[NSString alloc] initWithFormat:#"%#", event.eventIdentifier];
[arrayofCalIDs addObject:str];
To delete the events:
EKEventStore* store = [[[EKEventStore alloc] init] autorelease];
EKEvent* event2 = [store eventWithIdentifier:[arrayofCalIDs objectAtIndex:i]];
if (event2 != nil) {
NSError* error = nil;
[store removeEvent:event2 span:EKSpanThisEvent error:&error];
}
[myPath release];
Just an FYI for the answer above. It is found on the web on with this link:
http://tech.vniup.com/index.php/iphone/objective-c/how-to-delete-event-from-iphone-calendar-programmatically.html
My only suggestion is that if you are building an array of objects each object ideally would be the event. Then do a reverse array operation because the latest event will always be at the bottom.
NSMutableArray *reverseArray = [NSMutableArray arrayWithCapacity:[eventsList count]];
for (id element in [eventsList reverseObjectEnumerator]) {
[reverseArray addObject:element];
}
eventsList = reverseArray;
And also in the display of the events be nice to your users and display the start date of the event.
Anyway, after you have an array objects that are EKEvents you can do this which is MUCH easier.
EKEvent *eventToRemove = [myEventStore eventWithIdentifier:thisEvent.eventIdentifier ];
if ([eventToRemove.eventIdentifier length] > 0) {
NSError* error = nil;
[myEventStore removeEvent:eventToRemove span:EKSpanThisEvent error:&error];
}
Then you can remove that same event from you array of events for the table display....easy!