Search code examples
ioseventscalendareventkit

How do I add an Event Title (from a UI Text View) to a Calendar Event created by my APP?


I've developed an App and my App needs to create an event at device's calendar. I've been trough EventKitProgGuide and studied the SimpleEKDemo.

By simplifying the code from SimpleEKDemo I generated the code shown below that opens an 'calendar's event screen' straight from my app and generates the event properly. I'm OK with that.

Now I need to use the text content of an UITextView as the Event Title!

Could somebody help my with that code?

Thanks,

Marcos

Here's my code:

@.h
#import <EventKitUI/EventKitUI.h>
#import <EventKit/EventKit.h>

@property (nonatomic, strong) EKEventStore *eventStore;
@property (nonatomic, strong) EKEvent *event;
@property (nonatomic, strong) EKCalendar *defaultCalendar;
@property (nonatomic, strong) 
IBOutlet UITextView *textView1;
- (IBAction)agendar:(UIButton *)sender;

@.m

- (void)viewDidLoad
{
[super viewDidLoad];
self.eventStore = [[EKEventStore alloc]init];
self.textView1.text = @"hello world!";
}


-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self checkEventStoreAccessForCalendar];
}


- (IBAction)agendar:(UIButton *)sender {
EKEventEditViewController *addController = [[EKEventEditViewController alloc] init];
addController.eventStore = self.eventStore;
addController.editViewDelegate = self;
[self presentViewController:addController animated:YES completion:nil];
self.event = [EKEvent eventWithEventStore:self.eventStore];

// Jeff's suggested code:
self.event.title = self.textView1.text;

// Jeff's SaveEvent Sugestion
NSError *err;
[self.eventStore saveEvent:self.event span:EKSpanThisEvent error:&err];


}


-(void)checkEventStoreAccessForCalendar
{
EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];

switch (status)
{
    case EKAuthorizationStatusAuthorized: [self accessGrantedForCalendar];
        break;

    case EKAuthorizationStatusNotDetermined: [self requestCalendarAccess];
        break;


    case EKAuthorizationStatusDenied:
    case EKAuthorizationStatusRestricted:
    {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alerta de Privacidade" message:@"Permissão de acesso ao calendário não concedida."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
     [alert show];
    }
        break;
    default:
        break;
}
}


-(void)requestCalendarAccess
{
    [self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
     {
         if (granted)
         {
             Tela8ViewController * weakSelf = self;

             dispatch_async(dispatch_get_main_queue(), ^{

             [weakSelf accessGrantedForCalendar];
             });
         }
     }];
}


-(void)accessGrantedForCalendar
{
    self.defaultCalendar = self.eventStore.defaultCalendarForNewEvents;
}

- (void)eventEditViewController:(EKEventEditViewController *)controller
      didCompleteWithAction:(EKEventEditViewAction)action
{

    [self dismissViewControllerAnimated:YES completion:^
     {
         if (action != EKEventEditViewActionCanceled)
         {
              dispatch_async(dispatch_get_main_queue(), ^{

             });
         }
     }];
}

Solution

  • There are two ways to create an event with EventKit. Your sample code currently has a mix of both, so you should pick just one!

    A: Create an event with certain fields pre-set (in your case, the title), and allow the user to review and save it (or choose to cancel and discard it) with EKEventEditViewController. In this case, your code doesn't need to commit the event - just watch for the delegate response to confirm that it happened.

    - (void)createEventWithTitle:(NSString *)title
    {
        EKEvent *newEvent = [EKEvent eventWithEventStore:self.eventStore];
        newEvent.title = title;
    
        EKEventEditViewController *controller = [[EKEventEditViewController alloc] init];
        controller.eventStore = self.eventStore;
        controller.event = newEvent;
        controller.editViewDelegate = self;
    
        [self presentViewController:controller animated:YES completion:nil];
    }
    
    // EKEventEditViewController delegate method
    
    - (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action
    {
        if (action == EKEventEditViewActionSaved) {
            // event has been committed
        }
        // alternatives are EKEventEditViewActionCanceled, EKEventEditViewActionDeleted
        [self dismissViewControllerAnimated:YES completion:Nil];
    }
    

    B. You can create an event and commit it entirely in your code, if you don't need user involvement. In this case you can use EKEventStore saveEvent: span: error: instead of relying on EKEventEditViewController.