小胖说事----------调用系统日历,显示某一个时间并且弹出系统日历的添加事件页面

一、调用系统日历,显示某一个时间

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


如果要选择特定的某一天的话(默认是当前日期),calshow:后面加时间戳格式,也就是NSTimeInterva
注意这里计算时间戳调用的方法是-NSTimeInterval nowTimestamp = [[NSDate date] timeIntervalSinceReferenceDate];
timeIntervalSinceReferenceDate的参考时间是2000年1月1日,[NSDate date]是你希望跳到的日期。


二、弹出系统日历的添加事件页面

头文件引入:
#import <EventKit/EventKit.h>
#import <EventKitUI/EventKitUI.h>

实现代理:
EKEventEditViewDelegate

编写事件,传入EKEventEditViewController
EKEventStore *eventStore = [[EKEventStore alloc] init];
        __weak AUIViewController *weak = self;
            
        //判断是否可以查看日历
        if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
             //条用开启日历权限
             [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        if (granted) {
                            //获取权限成功
                            EKEvent *event = [EKEvent eventWithEventStore:eventStore];
                            
                            //设置标题和地理位置
                            event.title = @"哈哈哈,我是日历事件啊";
                            event.location = @"我在杭州西湖区留和路";
                            
                            //设置开始和结束时间
                            NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
                            [tempFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
                            event.startDate = [tempFormatter dateFromString:@"19.08.2016 22:30"];
                            event.endDate   = [tempFormatter dateFromString:@"20.08.2016 22:30"];
                            
                            //NO:添加行程时间  yes:不添加
                            event.allDay = YES;
                            
                            //添加提醒(有且只能添加两个提醒)
                            [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -60.0f * 24]];
                            [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -15.0f]];
                            
                            //设置日历事件的默认情况
                            [event setCalendar:[eventStore defaultCalendarForNewEvents]];
                            NSError *err;
                            [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
                            
                            //弹起视图
                            EKEventEditViewController *edit = [[EKEventEditViewController alloc]init];
                            edit.editViewDelegate = self;
                            edit.eventStore = eventStore;
                            edit.event = event;
                            [weak presentViewController:edit animated:YES completion:nil];
                        }else{
                            //获取权限失败
                            [UIAlertView showAlertViewWithMessage:@"获取权限失败"];
                        }
                    });
                }];
            }
实现代理方法
/**
 *  添加事件的代理方法
 *
 *  @param controller 事件的页面
 *  @param action     按钮点击的类型
 */
-(void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action
{
    //关闭页面
    [controller dismissViewControllerAnimated:YES completion:nil];
    if (action == EKEventEditViewActionCanceled) {
        //取消点击事件
    }else if (action == EKEventEditViewActionSaved){
        //保存点击事件
    }else if (action == EKEventEditViewActionDeleted){
        //删除点击事件
    }
}

大功告成!下面是运行图


另外,因为回调方法是在非UI线程中调用的,所以所有更新UI得操作都要在Main Thread调用,否则就会引发异常。

  EKEntityType

  这是个描述EventKit或者数据的类型 的枚举类型,它有两个值:Event、Reminder。它在很多方法中被使用,包括EventStore的RequestAccess方法,告诉EventKit要获取什么类型的数据权限。

  EKCalendar

  EKCalendar相当于一个日历,包含了一组日历事件,日历能够存储在很多地方,例如 本地、iCloud、第三方商:Exchange Sever 或者Google。大多时候,EKCalendar被用来告知EventKit,事件从何查找,存储到哪里。

  EKEventEditController

  EKEventEditController能够在MonoTouch.EventKitUI中找到,它是个内建的控制器,用于创建修改日历事件。这很像内建的相机控制器,EKEventEditController帮你做了繁重的UI和保存数据的工作。

  EKEvent

  EKEvent相当于一个日历,不管是EKEvent还是EKReminder都继承于EKCalendarItem,他们都有Title、Notes等字段。

  EKReminder

  EKReminder相当于一个提醒事项。

  EKSpan 

  EKSpan是个当修改事件为可重复的时候用于描述事件的跨度的枚举值,它有两个值:ThisEvent、FutureEvents。ThisEvent意味着任何改变,只会发生在被引用的一系列特定事件,而FutureEvents会影响事件和所有未来的复发。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值