如何将事件标题(从UI文本视图)添加到由我的APP创建的日历事件?

时间:2014-02-25 20:29:21

标签: ios events calendar eventkit

我开发了一款应用,我的应用需要在设备的日历中创建一个活动。我已经通过EventKitProgGuide并研究了SimpleEKDemo。

通过简化SimpleEKDemo中的代码,我生成了下面显示的代码,打开了一个日历的事件屏幕'直接从我的应用程序,并正确生成事件。我很好。

现在我需要使用UITextView的文本内容作为活动标题!

有人可以用我的代码帮助我吗?

谢谢,

马科斯

这是我的代码:

@.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(), ^{

             });
         }
     }];
}

2 个答案:

答案 0 :(得分:1)

使用EventKit创建事件有两种方法。您的示例代码目前混合使用,因此您只需选择一个!

答:创建一个预设某些字段的事件(在您的情况下,标题),并允许用户使用EKEventEditViewController查看并保存(或选择取消并丢弃它)。在这种情况下,您的代码不需要提交事件 - 只需注意委托响应以确认它发生了。

- (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中。如果您不需要用户参与,您可以创建一个事件并将其完全提交到您的代码中。在这种情况下,您可以使用EKEventStore saveEvent: span: error:而不是依赖于EKEventEditViewController。

答案 1 :(得分:0)

你有没有试过像

这样的东西
self.event.title = self.textView1.text;