subscribe to a url to add event in ical - iphone

I Want to add my events that is in my server into ical using eventkit. Can I subscribe to a url programatically so that all the event can be sync in ical app automatically.

Synchronization is done from your side (Inside objective "c"). You need to fetching all events from server after hitting appropriate URL.
Parse it and save single event with the help of below method:
-(void)addingEvents{
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = <<Event Title>>;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd hh:mma"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *strDate = [dateFormatter stringFromDate:<<Your event date>>];
NSDate *startDate = [dateFormatter dateFromString:strDate];
[dateFormatter release];
event.startDate = [[NSDate alloc] initWithTimeInterval:0 sinceDate:startDate];
event.endDate = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
NSArray *alertArray = [NSArray arrayWithObject:[EKAlarm alarmWithRelativeOffset:-300]];
event.alarms = alertArray;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
if (err) {
NSLog(#"Error occured : %#",[err localizedDescription]);
}
[eventStore release];
}

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.

adding events to default calendar in iPhone

i am trying to add the event to iPhone default calendar and the code is as follows
NSMutableString *startDateString = [NSMutableString stringWithString:#"11/20/2012 10:00 AM"];
NSMutableString *endtDateString = [NSMutableString stringWithString:#"11/20/2012 5:00 PM"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"MM-dd-yyyy HH:mm a"];
NSDate * startDate = [[NSDate alloc]init];
startDate = [dateFormatter dateFromString:startDateString];
NSDate *endDate = [[NSDate alloc]init];
endDate = [dateFormatter dateFromString:endtDateString];
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = #"TEST";
event.startDate = startDate;
event.endDate = endDate;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
[startDate release];
[endDate release];
if(!err)
{
UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:#"TEST" message:#"Event Added successfully " delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertview show];
[alertview release];
}
NSLog(#"error is %#",err);
}
the event is successfully adding but the problem is its not adding to event start date its just adding to jan 1st 2001 date all the events are adding to the same date only .
can any one please help me where i am getting wrong & i am using the iPhone 4s & iPhone 3gs with 5.1.1 version.
Thanks in advance .
Set DateFormat to dateFormatter like this:
[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
Note: yourdateString should be according to dateFormatter

How to store event in calender in iphone sdk?

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];
}

How can I change NSDateFormatter to "DD/MM/YYYY"?

I have tried putting this:
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = [NSString stringWithFormat:#"DHSB Assignment: %#", Assignment1.text];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"M/d"];
event.startDate = [formatter dateFromString:dateField.text];
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"M/d"];
event.endDate = [formatter2 dateFromString:dateField.text];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
[EKEventStore release];
NSLog(#"Successfully added '%#' to the calendar", Assignment1.text);
...but the event isn't added to the calendar if I enter a date in the format of "DD/MM/YYYY".
Why is this?
Thanks!
I think the correct format should be:
[formatter setDateFormat:#"dd/MM/yyyy"];
Try putting a NSLog on the dateFromString method to be sure you're getting the right result
EDIT: you may also need to set the correct locale for the date according to your needs.
I think your format string is wrong:
#"dd/MM/yyyy";
Might give better results.
By the way - what is the point of passing in an NSError* pointer to a function and not checking the return value or the error. Try replacing this line:
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
With something like this:
BOOL success = [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
if (!success) {
NSLog(#"Cannot save event. %#", [err localizedDescription];
}
Not perfect error checking but it will give you some idea of what is happening while you are developing.

date problem while adding Event to iPhone calendar

while adding an event to the calendar of an iphone, the date is going to be reseted to 1.1.2001 and i have no idea why.
id arg1 = [args objectAtIndex:0]; // start
id arg2 = [args objectAtIndex:1]; // end
id arg3 = [args objectAtIndex:2]; // title
id arg4 = [args objectAtIndex:3]; // location
id arg5 = [args objectAtIndex:4]; // text
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *startdate = [df dateFromString: arg1];
NSDate *enddate = [df dateFromString: arg2];
EKEventStore *eventStore = [[[EKEventStore alloc] init] autorelease];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = arg3;
event.location = arg4;
event.notes = arg5;
event.startDate = startdate;
event.endDate = enddate;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
the input seems to be fine since
NSLog([args objectAtIndex:0]);
writes the correct date. i have no real idea :(
Your input maybe correct but the formatter is not. Please, review these 2 lines. I believe the bug is there
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *startdate = [df dateFromString: arg1];
[df setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
is working fine so far.