如何在本机IOS日历中添加事件 [英] How to add event in native IOS Calendar

查看:122
本文介绍了如何在本机IOS日历中添加事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的应用程序中打开本机IOS日历(ical)并添加事件。
有什么方法可以打开特定活动的日历吗?

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?

我也跟着以编程方式打开iphone日历应用程序但尚未成功。

推荐答案

请参阅日历和提醒编程指南。但基本过程是:

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


  1. 添加 EventKit.Framework 和项目的 EventKitUI.Framework 。 (请参阅链接到图书馆或框架 。)

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

导入标题:

#import <EventKitUI/EventKitUI.h>


  • 如果要创建活动,请使用:

  • 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];
    }
    


  • 您的视图控制器应符合 EKEventEditViewDelegate 协议:

    @interface ViewController () <EKEventEditViewDelegate>
    

    并实施 didCompleteWithAction 方法:

    - (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    


  • 显然,您可以按照自己的方式创建活动。例如,这是在下周查找具有相应标题的事件,如果找不到,则创建一个新事件(在四小时内开始的一小时事件):

  • 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;
    }
    


  • 这篇关于如何在本机IOS日历中添加事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆