Add an event into iCal in iPhone application

后端 未结 2 857
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 04:29

There is a requirement in my application in which, when I take an appointment of a doctor for a particular day, that day should be added in iCal. and it should generate an a

相关标签:
2条回答
  • 2020-12-20 04:48

    Based on Apple Documentation, this has changed a bit as of iOS 6.0.

    1) You should request access to the user's calendar via "requestAccessToEntityType:completion:" and execute the event handling inside of a block.

    2) You need to commit your event now or pass the "commit" param to your save/remove call

    Everything else stays the same...

    Add the EventKit framework and #import to your code.

    To add an event:

       EKEventStore *store = [[EKEventStore alloc] init];
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (!granted) { return; }
        EKEvent *event = [EKEvent eventWithEventStore:store];
        event.title = @"Event Title";
        event.startDate = [NSDate date]; //today
        event.endDate = [event.startDate dateByAddingTimeInterval:60*60];  //set 1 hour meeting
        [event setCalendar:[store defaultCalendarForNewEvents]];
        NSError *err = nil;
        [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
        NSString *savedEventId = event.eventIdentifier;  //this is so you can access this event later
    }];
    

    Remove the event:

        EKEventStore* store = [[EKEventStore alloc] init];
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (!granted) { return; }
        EKEvent* eventToRemove = [store eventWithIdentifier:savedEventId];
        if (eventToRemove) {
            NSError* error = nil;
            [store removeEvent:eventToRemove span:EKSpanThisEvent commit:YES error:&error];
        }
    }];
    
    0 讨论(0)
  • 2020-12-20 04:55

    You need the EventKit framework. You can ask the EKEventStore for its calendars, and then use those calendars to create a predicate that lets you find events that match the criteria you're looking for. Or you can create a new EKEvent object and save it into the event store.

    0 讨论(0)
提交回复
热议问题