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


    一、打开系统的app
    1、打开日历:

    if UIApplication.sharedApplication().canOpenURL(NSURL(string:"calshow:”)!) {
    UIApplication.sharedApplication().openURL(NSURL(string:"calshow:”)!)
    }

    2、打开提醒事项:

    if UIApplication.sharedApplication().canOpenURL(NSURL(string:"x-apple-reminder://")!) {
    UIApplication.sharedApplication().openURL(NSURL(string:"x-apple-reminder://")!)
    }


    二、向系统的日历或提醒事项中添加信息:
    1、向日历中添加事件:

    1)、创建事件驱动器:
    let eventStore = EKEventStore()
    
    (2)、授权访问日历:
    eventStore.requestAccessToEntityType(.Event) { (result, erro) in }
    
    (3)、编辑事件:
    let event = EKEvent(eventStore: self.eventStore)
    // 事件名称
    event.title = "提醒" // 事件名称
    event.notes = "备注你妹" // 事件备注
    
    // 事件地点
    event.location = "东升科技园"
    
    // 这个设置为true 开始结束时间就不会显示
    event.allDay = true
    
    // 开始时间
    event.startDate = NSDate()
    // 结束时间
    event.endDate = NSDate(timeIntervalSinceNow: 3600)
    
    event.addAlarm(EKAlarm(relativeOffset: -60*15)) // 设置提醒
    
    // 必须设置:系统的日历
    event.calendar = self.eventStore.defaultCalendarForNewEvents
    
    (4)、添加事件:
    do {
    let _ = try self.eventStore.saveEvent(event, span: .ThisEvent)
    
    } catch {
    print(error)
    }

    2、向事项中添加事件:

    1)、创建事件驱动器:
    let eventStore = EKEventStore()
    
    (2)、授权访问提醒事项:
    eventStore.requestAccessToEntityType(.Reminder) { (result, erro) in }
    
    (3)、编辑事项:
    let reminde = EKReminder(eventStore: self.eventStore)
    
    // 事项名称
    reminde.title = "测试提醒"
    // 事项地点
    reminde.location = "东升科技园"
    
    // 事项等级:1-9,1最高
    reminde.priority = 1
    
    reminde.notes = "测试测试测试测试测试"
    
    // 事项开始事件
    let start = NSDateComponents()
    start.day = 5
    start.year = 2016
    start.month = 9
    start.hour = 22
    start.minute = 30
    start.timeZone = NSTimeZone.systemTimeZone()
    reminde.startDateComponents = start
    
    // 事项结束事件
    let end = NSDateComponents()
    end.day = 8
    end.year = 2016
    end.month = 9
    end.hour = 23
    end.minute = 30
    end.timeZone = NSTimeZone.localTimeZone()
    reminde.dueDateComponents = end
    // 说明:
    设置了结束时间,在事项中只会显示结束时间而不会显示开始时间
    
    reminde.addAlarm(EKAlarm(relativeOffset: -60*15)) // 添加提醒时间
    
    // 必须要设置:默认的事项提示app
    reminde.calendar = self.eventStore.defaultCalendarForNewReminders()
    
    (4)、添加事项:
    do {
    let _ = try self.eventStore.saveReminder(reminde, commit: true)
    
    } catch {
    print(error)
    }

    三、查找系统的日历或提醒事项中信息:

    1、查找日历事件:

    1)、创建事件驱动器:
    let eventStore = EKEventStore()
    
    (2)、授权访问日历:
    eventStore.requestAccessToEntityType(.Event) { (result, erro) in }
    
    (3)、查找事件:
    1)、创建查找条件
    let predicate = self.eventStore.predicateForEvents(withStart: Date(timeIntervalSinceNow: -3600), end: Date(), calendars: nil)
    
    2)、利用条件进行查找:由于是线程同步,耗时长,建议在其他线程操作
    查找方法1:
    let events = self.eventStore.events(matching: predicate)
    查找方法2:每找到一个就会调用闭包
    self.eventStore.enumerateEvents(matching: predicate, using: { (event,pointer) in})


    2、查找提醒事项:

    1)、创建事件驱动器:
    let eventStore = EKEventStore()
    
    (2)、授权访问提醒事项:
    eventStore.requestAccessToEntityType(.Reminder) { (result, erro) in }
    
    (3)、查找事件:
    1)、创建查找条件
    /// 获取所有的事项条件
    let pre0 = self.eventStore.predicateForReminders(in: nil)
    
    // 指定时间内未完成的事项条件
    let pre1 = self.eventStore.predicateForIncompleteReminders(withDueDateStarting: Date(timeIntervalSinceNow:3600), ending: Date(timeIntervalSinceNow:3600), calendars: nil)
    
    // 指定时间内已完成的事项条件
    let pre2 = self.eventStore.predicateForCompletedReminders(withCompletionDateStarting: Date(timeIntervalSinceNow:-3600), ending: Date(timeIntervalSinceNow:3600), calendars: nil)
    
    2)、利用条件进行查找:由于是线程同步,耗时长,建议在其他线程操作
    /// 获取事项
    let _ = self.eventStore.fetchReminders(matching: pre0, completion: { (reminders) in })


    四、删除系统的日历或提醒事项中信息:

    1、删除日历事件:

    do {
    let _ = try self.eventStore.remove(event, span: .thisEvent) 
    } catch {
    print(error)
    }


    2、删除事项提示:

    do {
    let _ = try self.eventStore.remove((reminders?.first)!, commit: true)
    } catch {
    print(error)
    }
  • 相关阅读:
    Blank page instead of the SharePoint Central Administration site
    BizTalk 2010 BAM Configure
    Use ODBA with Visio 2007
    Handling SOAP Exceptions in BizTalk Orchestrations
    BizTalk与WebMethods之间的EDI交换
    Append messages in BizTalk
    FTP protocol commands
    Using Dynamic Maps in BizTalk(From CodeProject)
    Synchronous To Asynchronous Flows Without An Orchestration的简单实现
    WSE3 and "Action for ultimate recipient is required but not present in the message."
  • 原文地址:https://www.cnblogs.com/jfckliving/p/5847404.html
Copyright © 2020-2023  润新知