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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
移动端iOS系统onload事件失效的问题可能是由于移动端浏览器的机制不同于桌面端浏览器,导致onload事件无法触发。此外,iOS系统的Safari浏览器还存在一些特殊的问题,如页面缓存、用户手势等问题,也可能导致onload事件失效。 解决这个问题的方法有多种,以下是一些常见的解决方法: 1. 使用window.onload事件代替onload事件 在移动端iOS系统中,window.onload事件可以替代onload事件,可以使用以下代码: ``` window.onload = function() { // Your code here }; ``` 2. 使用DOMContentLoaded事件 DOMContentLoaded事件会在DOM树结构构建完成后触发,不受页面资源加载的影响。可以使用以下代码: ``` document.addEventListener('DOMContentLoaded', function() { // Your code here }, false); ``` 3. 使用$(document).ready()事件 如果您使用jQuery库,可以使用$(document).ready()事件来代替onload事件。可以使用以下代码: ``` $(document).ready(function() { // Your code here }); ``` 4. 禁用页面缓存 iOS系统的Safari浏览器会将页面缓存下来,如果您的页面有缓存,onload事件可能不会触发。可以在HTML头部添加以下标签来禁用页面缓存: ``` <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"> <meta http-equiv="Pragma" content="no-cache"> <meta http-equiv="Expires" content="0"> ``` 5. 禁用用户手势 在iOS系统的Safari浏览器中,用户手势会触发页面刷新,导致onload事件失效。可以使用以下代码禁用用户手势: ``` document.addEventListener('touchmove', function(event) { event.preventDefault(); }, false); ``` 希望这些方法可以帮助您解决移动端iOS系统onload事件失效的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值