Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add event in native IOS Calendar

I want to open native IOS calendar(ical) from my application and add event. Is there any way i can open calendar for particular event?

I also follow Open iphone calendar app programmatically but not yet succeeded.

like image 863
Akshay Nalawade Avatar asked Apr 01 '13 11:04

Akshay Nalawade


People also ask

How can I add Indian Calendar in iOS?

Go to Settings > General > Language & Region, then tap Region. Tap a region, then tap Change to [region]. In Calendar, tap Calendars at the bottom of the screen.

Why can't I add a Calendar event on my iPhone?

Make Sure Calendars Are Synced On All Devices If you have more than one Apple device, then ensure that the calendar is synced across all your iOS, iPadOS, and macOS devices. For this, first of all, you have to sign in using the same Apple ID on all these devices.


2 Answers

See the Calendar and Reminders Programming Guide. But the basic process is:

  1. Add the EventKit.Framework and EventKitUI.Framework to your project. (See Linking to a Library or Framework.)

  2. Import the header:

    #import <EventKitUI/EventKitUI.h>
    
  3. If creating an event, you 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];
    }
    
  4. Your view controller should conform to the EKEventEditViewDelegate protocol:

    @interface ViewController () <EKEventEditViewDelegate>
    

    and implement the didCompleteWithAction method:

    - (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
  5. You can obviously create your event any way you want. For example, this is looks for an event in the next week with the appropriate title, and if it doesn't find it, create a new event (hour long event that starts in four hours):

    - (EKEvent *)findOrCreateEvent:(EKEventStore *)store
    {
        NSString *title = @"My event title";
    
        // try to find an event
    
        EKEvent *event = [self findEventWithTitle:title inEventStore:store];
    
        // if found, use it
    
        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
    {
        // Get the appropriate calendar
        NSCalendar *calendar = [NSCalendar currentCalendar];
    
        // Create the start range date components
        NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
        oneDayAgoComponents.day = -1;
        NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
                                                      toDate:[NSDate date]
                                                     options:0];
    
        // Create the end range date components
        NSDateComponents *oneWeekFromNowComponents = [[NSDateComponents alloc] init];
        oneWeekFromNowComponents.day = 7;
        NSDate *oneWeekFromNow = [calendar dateByAddingComponents:oneWeekFromNowComponents
                                                           toDate:[NSDate date]
                                                          options:0];
    
        // Create the predicate from the event store's instance method
        NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo
                                                                endDate:oneWeekFromNow
                                                              calendars:nil];
    
        // Fetch all events that match the predicate
        NSArray *events = [store eventsMatchingPredicate:predicate];
    
        for (EKEvent *event in events)
        {
            if ([title isEqualToString:event.title])
            {
                return event;
            }
        }
    
        return nil;
    }
    
like image 123
Rob Avatar answered Sep 28 '22 05:09

Rob


Use url scheme to open the native calender app.

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"calshow://"]];

http://devsupport.layar.com/entries/23687993-How-to-use-Custom-URI-Scheme-Button-in-the-Creator-

like image 35
nswamy Avatar answered Sep 28 '22 04:09

nswamy