icalendar - How to add event in native IOS Calendar -
i want open native ios calendar(ical) application , add event. there way can open calendar particular event?
i follow open iphone calendar app programmatically not yet succeeded.
see calendar , reminders programming guide. basic process is:
add
eventkit.framework
,eventkitui.framework
project. (see linking library or framework.)import header:
#import <eventkitui/eventkitui.h>
if creating event, use :
- (ibaction)didpresscreateeventbutton:(id)sender { ekeventstore *store = [[ekeventstore alloc] init]; if([store respondstoselector:@selector(requestaccesstoentitytype:completion:)]) { // ios 6 [store requestaccesstoentitytype:ekentitytypeevent completion:^(bool granted, nserror *error) { if (granted) { dispatch_async(dispatch_get_main_queue(), ^{ [self createeventandpresentviewcontroller:store]; }); } }]; } else { // ios 5 [self createeventandpresentviewcontroller:store]; } } - (void)createeventandpresentviewcontroller:(ekeventstore *)store { ekevent *event = [self findorcreateevent:store]; ekeventeditviewcontroller *controller = [[ekeventeditviewcontroller alloc] init]; controller.event = event; controller.eventstore = store; controller.editviewdelegate = self; [self presentviewcontroller:controller animated:yes completion:nil]; }
your view controller should conform
ekeventeditviewdelegate
protocol:@interface viewcontroller () <ekeventeditviewdelegate>
and implement
didcompletewithaction
method:- (void)eventeditviewcontroller:(ekeventeditviewcontroller *)controller didcompletewithaction:(ekeventeditviewaction)action { [self dismissviewcontrolleranimated:yes completion:nil]; }
you can create event way want. example, looks event in next week appropriate title, , if doesn't find it, create new event (hour long event starts in 4 hours):
- (ekevent *)findorcreateevent:(ekeventstore *)store { nsstring *title = @"my event title"; // try find event ekevent *event = [self findeventwithtitle:title ineventstore:store]; // if found, use if (event) return event; // if not, let's create new event event = [ekevent eventwitheventstore:store]; event.title = title; event.notes = @"my event notes"; event.location = @"my event location"; event.calendar = [store defaultcalendarfornewevents]; nscalendar *calendar = [nscalendar currentcalendar]; nsdatecomponents *components = [[nsdatecomponents alloc] init]; components.hour = 4; event.startdate = [calendar datebyaddingcomponents:components todate:[nsdate date] options:0]; components.hour = 1; event.enddate = [calendar datebyaddingcomponents:components todate:event.startdate options:0]; return event; } - (ekevent *)findeventwithtitle:(nsstring *)title ineventstore:(ekeventstore *)store { // appropriate calendar nscalendar *calendar = [nscalendar currentcalendar]; // create start range date components nsdatecomponents *onedayagocomponents = [[nsdatecomponents alloc] init]; onedayagocomponents.day = -1; nsdate *onedayago = [calendar datebyaddingcomponents:onedayagocomponents todate:[nsdate date] options:0]; // create end range date components nsdatecomponents *oneweekfromnowcomponents = [[nsdatecomponents alloc] init]; oneweekfromnowcomponents.day = 7; nsdate *oneweekfromnow = [calendar datebyaddingcomponents:oneweekfromnowcomponents todate:[nsdate date] options:0]; // create predicate event store's instance method nspredicate *predicate = [store predicateforeventswithstartdate:onedayago enddate:oneweekfromnow calendars:nil]; // fetch events match predicate nsarray *events = [store eventsmatchingpredicate:predicate]; (ekevent *event in events) { if ([title isequaltostring:event.title]) { return event; } } return nil; }
Comments
Post a Comment