I am using the following code to add an event to my calendar, but it's not showing any error neither saving into my calendar. Please help.
EKEventStore *eventstore = [[EKEventStore alloc] init];
if([eventstore respondsToSelector:#selector(requestAccessToEntityType:completion:)])
{
[eventstore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if(granted)
{
// if user granted your app to use calendar control will reach over hare
// add the code to add event to iCal that you are using previously for iOS5
EKEvent *event = [EKEvent eventWithEventStore: eventstore];
event.title = #"Title";
event.notes = #"Description";
event.location = #"Location";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormat setTimeZone:timeZone];
[dateFormat setDateFormat:#"dd-MM-yyyy hh:mm a"];
NSDate *date = [dateFormat dateFromString:[NSString stringWithFormat:#"%#",[[eventsArray objectAtIndex:index] objectForKey:#"EventDate"]]];
event.startDate =date;
event.endDate = [date dateByAddingTimeInterval:36000];;
event.availability = EKEventAvailabilityFree;
[event setCalendar:[eventstore defaultCalendarForNewEvents]];
NSError *err;
[eventstore saveEvent:event span:EKSpanThisEvent error:&err];
}
else
{
NSLog(#"Calender not called");
// if user did not your app to use calendar control will reach over hare
}
}];
}
First i have added eventkit framework, then #import EventKit/EventKit.h into <> added to my view controller, and then implemented a code as below, and it worked fine for me hope you can also find solution by referring this :)
-(IBAction)AddEvent:(id)sender
{
EKEventStore *eventStore = [[EKEventStore alloc] init];
if([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)]) {
// iOS 6 and later
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted){
//---- codes here when user allow your app to access theirs' calendar.
[self performCalendarActivity:eventStore];
}else
{
//----- codes here when user NOT allow your app to access the calendar.
}
}];
}
else {
//---- codes here for IOS < 6.0.
[self performCalendarActivity:eventStore];
}
}
-(void)performCalendarActivity:(EKEventStore *)eventStore
{
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
if([txtTitle.text isEqualToString:#""] || txtTitle.text == NULL)
txtTitle.text=#"Event Title";
event.title= txtTitle.text;
NSDate *duedate = pkrDate.date;
event.startDate =duedate;
event.endDate= [[NSDate alloc] initWithTimeInterval:600 sinceDate:duedate];
if(switchAlarm.on==TRUE){
NSArray *arrAlarm = [NSArray arrayWithObject:[EKAlarm alarmWithAbsoluteDate:duedate]];
event.alarms= arrAlarm;
}
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
BOOL isSuceess=[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
if(isSuceess){
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:#"Event" message:#"Event added in calendar" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertview show];
[alertview release];
}
else{
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:#"Event" message:[err description] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertview show];
[alertview release];
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
I had same problem no errors but no event in calendar.
Check iphone settings:icloud:calendars switch is turned ON.
Related
I need to set the alarm with notification in my App I have try the following codes but no luck for me
EKEventStore * eventStore1 = [[EKEventStore alloc] init];
if([eventStore1 respondsToSelector:#selector(requestAccessToEntityType:completion:)]) {
[eventStore1 requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
// This code will run when uses has made his/her choice
EKEvent *event = [EKEvent eventWithEventStore:eventStore1];
NSDate * date = [NSDate date];
NSLog(#"%#",date);
event.title = #"HAIManiiiiii";
event.location = #" ";
event.startDate = date;
event.endDate = [[[NSDate alloc] initWithTimeInterval:36 sinceDate:event.startDate] autorelease];
EKRecurrenceRule *rule = [[EKRecurrenceRule alloc]
initRecurrenceWithFrequency:EKRecurrenceFrequencyYearly
interval:1
end:[EKRecurrenceEnd recurrenceEndWithOccurrenceCount:10]];
EKAlarm *alarm1 = [EKAlarm alarmWithRelativeOffset:-36];
NSArray * ruleArray = [[NSArray alloc]initWithObjects:rule, nil];
NSArray * alrmArray = [[NSArray alloc] initWithObjects: alarm1, nil];
event.alarms = alrmArray;
event.recurrenceRules = ruleArray;
[event setCalendar:[eventStore1 defaultCalendarForNewEvents]];
NSError *err;
// [eventStore1 saveEvent:event span:EKSpanThisEvent error:&err];
NSLog(#"%d",[eventStore1 saveEvent:event span:EKSpanThisEvent error:&err]);
NSLog(#"Error From iCal : %#", [err description]);
}]
The event and is adding to the calendar by its not working for me.Please suggest me some idea to fix this issue
Use following code. It probably worked fine for me.
EKEventStore *eventStore = [[[EKEventStore alloc] init] autorelease];
if([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)]) {
// iOS 6 and later
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted){
//---- codes here when user allow your app to access theirs' calendar.
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"My Event";
event.notes =#"";
NSLog(#"%#",[StartTimePicker date]);
event.startDate = [StartTimePicker date];
// event.startDate = delegate.SelectedDate;
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
EKAlarm *alarm=[EKAlarm alarmWithRelativeOffset:600];
[event addAlarm:alarm];
BOOL Save= [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSLog(#"%d",Save);
if (Save) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Success" message:#"Alarm set" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
[DejalBezelActivityView removeViewAnimated:YES];
}
}else
{
[DejalBezelActivityView removeViewAnimated:YES];
//----- codes here when user NOT allow your app to access the calendar.
NSLog(#"Permission not allowed");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Permissions not allowed" message:#"Please allow access permission in Calendar setting" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}];
}
else {
//---- codes here for IOS < 6.0.
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"My Event";
event.startDate =dt;
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
EKAlarm *alarm=[EKAlarm alarmWithRelativeOffset:600];
[event addAlarm:alarm];
BOOL Save= [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
NSLog(#"%d",Save);
if (Save) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Success" message:#"Alarm set" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}
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.
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];
EKEventStore *eventDB = [[EKEventStore alloc] init];
EKEvent *myEvent = [EKEvent eventWithEventStore:eventDB];
NSString *trimmedString = [[arrDateList objectAtIndex:i] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
myEvent.title = [d valueForKey:#"garden_name"];
myEvent.startDate = [dateFormatter dateFromString:trimmedString];
myEvent.endDate = [dateFormatter dateFromString:trimmedString];
myEvent.allDay = TRUE;
[myEvent setCalendar:[eventDB defaultCalendarForNewEvents]];
NSError *err;
[eventDB saveEvent:myEvent span:EKSpanThisEvent error:&err];
I am using the above line of code but i cant store the event in calender...
please help me where i have mistake in line of code....
Thanks & regards
Rahul Virja
you can try this code..
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm a"];
NSDate *date = [dateFormatter dateFromString:[taskDetails taskStartDateTime]];
if DEBUG
NSLog(#"here is the task start %#",[taskDetails taskStartDateTime]);
endif
NSDate *end = [dateFormatter dateFromString:[taskDetails taskEndDateTime]];
[dateFormatter release];
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = [taskDetails taskGottaDo];
event.startDate = date;
event.endDate = end;
EKAlarm *reminder = [EKAlarm alarmWithRelativeOffset:-6*60*60];
[event addAlarm:reminder];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSTimeInterval alarmOffset = -1*60;//1 hour
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:alarmOffset];
[event addAlarm:alarm];
[eventStore release];
NSError *err;
BOOL saved = [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
// NSLog(#"here is the error %#",[eventStore saveEvent:event span:EKSpanThisEvent error:&err]);
if (saved == YES)
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#""
message:#"Saved to calendar"
delegate:nil
cancelButtonTitle:#"Right On!" otherButtonTitles:nil] ;
[alertView show];
[alertView release];
}
Can anyone have any idea about programmatically add reminder to iPhone calendar.
I searched the iPhone SDK documentation but didn't find anything.
Thanks and Regards
Jayaraj
This functionality is available in iOS 4.x with EventKit.
You could try to read this EventKit sample.
Oh yes you can...
#import <EventKit/EventKit.h>
...
EKEventStore *eventDB = [[EKEventStore alloc] init];
EKEvent *myEvent = [EKEvent eventWithEventStore:eventDB];
myEvent.title = #"New Event";
myEvent.startDate = [[NSDate alloc] init];
myEvent.endDate = [[NSDate alloc] init];
myEvent.allDay = NO;
myEvent.notes = #"Test";
[myEvent setCalendar:[eventDB defaultCalendarForNewEvents]];
NSError *err;
[eventDB saveEvent:myEvent span:EKSpanThisEvent error:&err];
if (err == noErr) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Event Created"
message:#"Yay!?"
delegate:nil
cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[alert show];
[alert release];
}
You can't. If you need this functionality you should file a bug with Apple explaining why you need it.