14

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

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
WildWorld
  • 517
  • 1
  • 5
  • 18

2 Answers2

24

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/

biddulph.r
  • 5,226
  • 3
  • 32
  • 47
BP.
  • 10,033
  • 4
  • 34
  • 53
  • thank you :) for now it works fine. i will get back at you asap i test it on the iPhone. i have on question, what is the else on the end of the code good fore ? i know the first if else if and else. not sure about the last. – WildWorld Oct 27 '12 at 14:18
  • I have edited the code above to further explain what is going on. The else part will run if the app is running on iOS 4 or 5, as on those versions of the OS, that event kit selector will not exist, and you will still want your important stuff to be done in that instance. – BP. Oct 28 '12 at 22:54
  • And of course if it is working for you, you can always click the check mark to the left of the answer to accept it. ;D – BP. Oct 28 '12 at 22:54
  • Thank you very much, but what do I have to add to make an alarm sound for my event? And if possible, how to add a custom alarm sound for the event? – lenhhoxung Nov 11 '13 at 06:34
2

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];
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81