iOS 添加事件到系统日历中

-(void)saveEventToCalendarTomorrow:(id)event{

    //事件市场

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

    

    //6.0及以上通过下面方式写入事件

    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

                    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提醒" message:@"请设置本应用日历权限" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

                    [alert show];

                }

                else

                {

                    // access granted

                    // ***** do the important stuff here *****

                    //事件保存到日历

                    //创建事件

                    EKEvent *event  = [EKEvent eventWithEventStore:eventStore];

                    event.title     = @"我是日历事件啊";

                    event.location = @"我在shanghai";

                    event.notes =@"notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notes notes notes notes notes notes notes notes notesnotes notes notes notes notes notesnotes notes notes notes notes notes notes notesnotes notes notes notes notes notes";

                    //日历中显示的url

                    event.URL=[NSURL URLWithString:@"http://www.baidu.com"];

                    NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];

                    [tempFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];

                    //创建一个时间段的日历事件

                    NSString *startDateStr=@"08.07.2016 10:10";

                    NSString *endDateStr=@"08.07.2016 11:50";

                    event.startDate=[tempFormatter dateFromString:startDateStr];

                    event.endDate=[tempFormatter dateFromString:endDateStr];

                    //创建一个一天的日历事件

//                    event.startDate = [[NSDate alloc]init ];

//                    event.endDate   = [[NSDate alloc]init ];

//                    event.allDay = YES;

                    

//                    NSDate *currentDate=[NSDate date];

//                    NSLog(@"Current Date is %@ ",[tempFormatter stringFromDate:currentDate]);

//                    //添加提醒

//                    NSDate *laterDate=[NSDate dateWithTimeIntervalSinceNow:60];

//                    NSLog(@"laterDate Date is %@ ",[tempFormatter stringFromDate:laterDate]);

                    NSString *alarmDateStr=@"08.07.2016 07:10";

                    

                    //                    [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -60.0f * 24]];

                    //                    [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -15.0f]];

                    //给事件 添加一个提醒  这样显示的是事件前多长时间提醒  比如1小时前

                    [event addAlarm:[EKAlarm alarmWithRelativeOffset:-(60*60)]];

                    //这样显示的就是具体什么时候开始提醒

//                    [event addAlarm:[EKAlarm alarmWithAbsoluteDate:[tempFormatter dateFromString:alarmDateStr]]];

                    [event setCalendar:[eventStore defaultCalendarForNewEvents]];

                    NSError *err;

                    //需要保存事件  才会添加到日历中去

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

                    

                    UIAlertView *alert = [[UIAlertView alloc]

                                          initWithTitle:@"提醒"

                                          message:@"创建成功?"

                                          delegate:nil

                                          cancelButtonTitle:@"确定"

                                          otherButtonTitles:nil];

                    [alert show];

                    

                    NSLog(@"保存成功");

                    

                }

            });

        }];

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值