2

I have a scenario where the user clicks on a UIButton and the event is automatically added to the my iCal. after importing the #import framework and adding the EKEventEditViewDelegate , how should I have to add the event start date and title etc. All this info is downloaded from the server as text file. Is there any functions that can help me set event date time and title? I shouldn't opening the EventsViewController but do it in the backend. Any suggestions?

Siddharthan Asokan
  • 4,321
  • 11
  • 44
  • 80

1 Answers1

4

I just set EKEvent startDate and endDate. I don't know what you mean "do it in the backend"...

- (BOOL)createEvent:(NSString *)title 
                 at:(NSString *)location 
           starting:(NSDate *)startDate 
             ending:(NSDate *)endDate 
           withBody:(NSString *)body 
             andUrl:(NSURL *)url
{

    EKEventStore *eventStore = [[EKEventStore alloc] init];

    EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
    event.title     = title;
    event.location  = location;
    event.startDate = startDate;
    event.endDate   = endDate;
    event.notes     = body;
    if (url)
        event.URL   = url;

    [event setCalendar:[eventStore defaultCalendarForNewEvents]];

    EKEventEditViewController *eventViewController = [[EKEventEditViewController alloc] init];
    eventViewController.event = event;
    eventViewController.eventStore = eventStore;
    eventViewController.editViewDelegate = self;

    [_viewController presentModalViewController:eventViewController animated:YES];

    return TRUE;
}

If you want to convert the date strings from your server into NSDate objects, NSDateFormatter generally does the job (assuming the server date text strings are in a well defined format). E.g., it might work something like:

NSString *sampleDate = @"7/23/12 2:30 pm";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"M/d/y h:mm a";

NSDate *date = [formatter dateFromString:sampleDate];
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • What if i don't want to see the view controller? The event adding takes place on pressing a button. This function will add event from the text file information. – Siddharthan Asokan Jul 23 '12 at 05:36
  • @SiddharthanAsokan I don't know if you can. It could be abused so easily. But perhaps you can, I'm not entirely sure. And while my initial reaction was the same as yours (gee, do I really need to?!?), from a usability perspective, it turns out that it is actually quite nice to let the user to tweak the entry as they see fit. I don't think that this is just a matter of making lemonade from lemons, but it might... – Rob Jul 23 '12 at 05:40
  • Haha I agree. btw Events.alarm = _____ does it takes strings? or NSDate? – Siddharthan Asokan Jul 23 '12 at 05:45
  • 1
    The `alarms` is an array of [`EKAlarm`](http://developer.apple.com/library/ios/#documentation/EventKit/Reference/EKAlarmClassRef/EKAlarmClassReference/EKAlarmClassReference.html#//apple_ref/occ/cl/EKAlarm) objects. Looks like you can use either `NSTimeInterval` (a number of seconds) or a `NSDate`, though I've never used it, so I can't speak from experience on that one. – Rob Jul 23 '12 at 05:53
  • @Rob: please check this http://stackoverflow.com/questions/36033167/creating-multiple-event-on-calendar-using-background-thread – Kunal Kumar Mar 16 '16 at 12:07