美文网首页
【Swift4.0】 动态添加手机提醒事项

【Swift4.0】 动态添加手机提醒事项

作者: 晴天mk1992 | 来源:发表于2018-01-10 02:00 被阅读0次

1、首先要在info.plist里面授权app可以使用日历
Privacy - Reminders Usage Description

2、导入需要的事件库:EventKit.framework

    var eventStore: EKEventStore!
    @IBAction func onAddAlarmClick(_ sender: Any) {
        self.eventStore = EKEventStore()
        let formatter = DateFormatter.init()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let date = formatter.date(from: "2018-01-10 01:57:00")
        self.eventStore.requestAccess(to: .reminder, completion: { _,error in
            let reminder = EKReminder.init(eventStore: self.eventStore)
            reminder.title = "今天要运动"
            let dueDate = self. dateComponentFromDate(date!)
            reminder.dueDateComponents = dueDate
            reminder.calendar = self.eventStore.defaultCalendarForNewReminders();
//            添加闹钟
            let alarm = EKAlarm.init(relativeOffset: -5)
            reminder.addAlarm(alarm);
            do {
//                try self.eventStore.save(event, span: span)
                try self.eventStore.save(reminder, commit: true)
                print("保存成!")
            }catch{
                print("创建失败: \(error)")
            }
        })
        
    }
    //根据NSDate获取对应的NSDateComponents对象
    func dateComponentFromDate(_ date: Date)-> DateComponents{
        let calendarUnit: Set<Calendar.Component> = [.minute, .hour, .day, .month, .year]
        let dateComponents = NSCalendar.current.dateComponents(calendarUnit, from: date)
        return dateComponents
    }

相关文章

  • 【Swift4.0】 动态添加手机提醒事项

    1、首先要在info.plist里面授权app可以使用日历Privacy - Reminders Usage De...

  • EventKit 日历事件

    EventKit 框架 此框架可以访问手机系统日历和提醒事项APP的数据,用此框架可以添加日历事件和提醒事项。 i...

  • swift版-系统的日历和提醒事项的调用与交互

    一、打开系统的app 二、向系统的日历或提醒事项中添加信息:1、向日历中添加事件: 2、向事项中添加事件: 三、查...

  • Swift4 - KVO的浅析

    SWIFT4.0中的KVO 在swift4.0 中 KVO 出现了变更,添加了监听回调.变得更加简单易用.同时会自...

  • iOS EventKit 添加日历及提醒事项(重复提醒)

    EventKit是在iOS和OS X下都可以使用的框架。今天主要展示的是如何给提醒事件添加重复提醒。 简单来说EK...

  • 你好,24岁

    23:53 在我听着外面风呼啸而过的时候 手机提醒事项响起来了 “BB!!” 三天前我写的提醒事项名字是这个 其实...

  • 10813健康为上

    一直没关手机上的提醒事项,它孜孜不倦地提醒着我“提醒吃药”。它提醒我,我提醒那个患癫痫的孩子。 这是去年他父亲决定...

  • 微信小程序如何动态添加样式

    动态添加style 动态添加class

  • 皮具保养

    手机储存空间满了,发个动态来保存。也提醒大家保养皮具的方法。

  • Axure案例——Toast

    Axure案例——Toast 制作流程: 1、快速制作手机页面,添加toast动态面板,设置两个状态; 2、添加刷...

网友评论

      本文标题:【Swift4.0】 动态添加手机提醒事项

      本文链接:https://www.haomeiwen.com/subject/hhennxtx.html