美文网首页
iOS 绑定系统日历,并且添加提醒事件

iOS 绑定系统日历,并且添加提醒事件

作者: 彭磊PL | 来源:发表于2020-09-07 16:56 被阅读0次

    好久没有更新简书啦,最近沉迷于加班,无法自拔,现在程序媛上线了~

    一、背景

    前几天有个需求,需要用户点击一个开启日历的按钮,然后将当前的事件和系统日历绑定,并且在连续的几天的中午12点给用户提醒消息,OK,废话不多说,上代码

    二、实现方式

    1、首先在info.plist文件中添加权限

    Privacy - Calendars Usage Description 访问日历的权限
    Privacy - Reminders Usage Description 访问提醒事件的权限

    2、判断是否有权限

    class EventCalendar: NSObject {
        //单例
        static let eventStore = EKEventStore()
        
        ///用户是否授权使用日历
        func isEventStatus() -> Bool {
            let eventStatus = EKEventStore.authorizationStatus(for: EKEntityType.event)
            if eventStatus == .denied || eventStatus == .restricted {
                return false
            }
            return true
        }
    }
    
    EventCalendar.eventStore.requestAccess(to: EKEntityType.event) { [unowned self] (granted, error) in
    //用户没授权
                    if !granted {
                        let alertViewController = UIAlertController(title: "提示", message: "请在iPhone的\"设置->隐私->日历\"选项中,允许***访问你的日历。", preferredStyle: .alert)
                        let actionCancel = UIAlertAction(title: "取消", style: .cancel, handler: { (action) in
                        })
                        let actinSure = UIAlertAction(title: "设置", style: .default, handler: { (action) in
                            //跳转到系统设置主页
                            if let url = URL(string: UIApplicationOpenSettingsURLString) {
                                //根据iOS系统版本,分别处理
                                if #available(iOS 10, *) {
                                    UIApplication.shared.open(url)
                                } else {
                                    UIApplication.shared.openURL(url)
                                }
                            }
                        })
                        alertViewController.addAction(actionCancel)
                        alertViewController.addAction(actinSure)
                        self.present(alertViewController, animated: true, completion: nil)
                        return
                    }
    }
    

    3、获取第二天中午12点的date

    func getTomorrowDate(_ timeCount: Int, dayCount: Int) -> Date {
            let calendar = NSCalendar.current
            var componts = calendar.dateComponents([.weekday, .year, .month, .day], from: Date())
            guard let day = componts.day else {
                return Date()
            }
            componts.day = day + dayCount
            let tomorrowInterval: TimeInterval = TimeInterval((calendar.date(from: componts)?.timeIntervalSince1970 ?? 0) + TimeInterval(timeCount * 60 * 60))
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            let tomorrowDate = Date(timeIntervalSince1970: tomorrowInterval)
            return tomorrowDate
        }
    

    最后附上Demo

    相关文章

      网友评论

          本文标题:iOS 绑定系统日历,并且添加提醒事件

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