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];
}
}
Related
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.
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.
Event is successfully added with alarm that plays ten seconds (for testing purposes) before event start time. Problem is that alarm triggers (UIAlertView on iPhone shows) but it dissapears in less then a second. Very strange...
What could be the problem?
Code:
- (IBAction)addEvent:(id)sender {
EKEventStore *eventStore = [[EKEventStore alloc] init];
if ([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)])
{
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
EKEvent *anEvent = [EKEvent eventWithEventStore:eventStore];
anEvent.calendar = eventStore.defaultCalendarForNewEvents;
anEvent.title = self.eventNameTextField.text;
NSDate *date = self.datePicker.date;
anEvent.startDate = date;
anEvent.endDate = date;
NSMutableArray *myAlarmsArray = [[NSMutableArray alloc] init];
EKAlarm *alarm1 = [EKAlarm alarmWithRelativeOffset:-10];
[myAlarmsArray addObject:alarm1];
anEvent.alarms = myAlarmsArray;
NSError *eventError;
[eventStore saveEvent:anEvent span:EKSpanThisEvent error:&eventError];
}];
}
}
see the following code hope it will help you.
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = [NSString stringWithFormat:#"%#'s Birthday",strName];
NSDate *twoYearsFromNow = [NSDate dateWithTimeIntervalSinceNow:1577846275];
double alarmAmountInSeconds = 60*60*9;
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:(1.0*alarmAmountInSeconds)];
EKRecurrenceRule *recurrance;
recurrance = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:EKRecurrenceFrequencyYearly interval:1 end:[EKRecurrenceEnd recurrenceEndWithEndDate:twoYearsFromNow]];
NSMutableArray *arrayReccurence = [NSMutableArray arrayWithObject:recurrance];
event.recurrenceRules = arrayReccurence;
event.startDate = startDate;
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
event.allDay = true;
event.alarms = [NSArray arrayWithObject:alarm];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
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.