iOS添加事件到系统日历

最近在项目中需要接触在工程中接受到某个时间后将其解析后写入iPhone系统自带日历中,在通过研究文档与资料后实现,特此记录:

首先: 看下效果图:

这里写图片描述

项目中调用到系统库 EventKit.framework.
利用苹果提供的接口完全可以实现此功能.下面贴上核心代码:

导入头文件

#import <EventKit/EventKit.h>

demo中是通过按钮点击事件来执行写入日历操作.实际项目中一般通过监听事件执行:

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加点击按钮
    UIButton * button = [[UIButton alloc]init];
    button.frame = CGRectMake(0, 0, 100, 40);
    button.center = CGPointMake(self.view.frame.size.width*0.5  , self.view.frame.size.height * 0.5);
    button.backgroundColor = [UIColor redColor];
    [button setTitle:@"写日历" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(saveEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

}

//点击事件处理

-(void)saveEvent:(id)sender
{
//calshow:后面加时间戳格式,也就是NSTimeInterval
//    注意这里计算时间戳调用的方法是-
//    NSTimeInterval nowTimestamp = [[NSDate date] timeIntervalSinceDate:2016];

//    timeIntervalSinceReferenceDate的参考时间是2000年1月1日,
//    [NSDate date]是你希望跳到的日期。


    EKEventStore *eventStore = [[EKEventStore alloc] init];

    //06.07 使用 requestAccessToEntityType:completion: 方法请求使用用户的日历数据库

    if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
    {
        // the selector is available, so we must be on iOS 6 or newer
        [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (error)
                {
                    //错误细心
                    // display error message here
                }
                else if (!granted)
                {
                    //被用户拒绝,不允许访问日历
                    // display access denied error message here
                }
                else
                {
                    // access granted
                    // ***** do the important stuff here *****

                    //事件保存到日历
                    //06.07 元素
                    //title(标题 NSString),
                    //location(位置NSString),
                    //startDate(开始时间 2016/06/07 11:14AM),
                    //endDate(结束时间 2016/06/07 11:14AM),
                    //addAlarm(提醒时间 2016/06/07 11:14AM),
                    //notes(备注类容NSString)

                    //创建事件
                    EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
                    event.title  = @"测试写入日历事件";
                    event.location = @"北京海淀";

//                    NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
//                    [tempFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];

                    //06.07 时间格式
                    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
                    [dateFormatter setAMSymbol:@"AM"];
                    [dateFormatter setPMSymbol:@"PM"];
                    [dateFormatter setDateFormat:@"yyyy/MM/dd hh:mmaaa"];
                    NSDate *date = [NSDate date];
                    NSString * s = [dateFormatter stringFromDate:date];
                    NSLog(@"%@",s);

                    //开始时间(必须传)
                    event.startDate = [date dateByAddingTimeInterval:60 * 2];
                    //结束时间(必须传)
                    event.endDate   = [date dateByAddingTimeInterval:60 * 5 * 24];
//                    event.endDate   = [[NSDate alloc]init];
//                    event.allDay = YES;//全天

                    //添加提醒
                    //第一次提醒  (几分钟后)
                    [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -1.0f]];
                    //第二次提醒  ()
//                    [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -10.0f * 24]];

                    //06.07 add 事件类容备注
                    NSString * str = @"接受信息类容备注";
                    event.notes = [NSString stringWithFormat:@"%@",str];

                    [event setCalendar:[eventStore defaultCalendarForNewEvents]];
                    NSError *err;

                    [eventStore saveEvent:event span:EKSpanThisEvent error:&err];

                    NSLog(@"保存成功");

                    //直接杀死进程
                    exit(2);

                }
            });
        }];
    }


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

在点击执行后通过exit(2)直接杀死进程.所以此时去日历中就可以看到刚才的事件写入成功了.

附上demo地址:
https://github.com/guanalongaaa/calendarDemo#calendardemo

感谢http://www.cnblogs.com/xiaobaichangan/p/5160025.html提供帮助.

下面是一些官方文档有兴趣可以看下:

本文档是下面的示例代码和参考手册的配套指南:
https://developer.apple.com/library/ios/samplecode/SimpleEKDemo/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010160
https://developer.apple.com/library/ios/samplecode/SimpleEKDemo/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010160
https://developer.apple.com/library/ios/documentation/EventKitUI/Reference/EventKitUIFrameworkRef/index.html#//apple_ref/doc/uid/TP40009663

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值