在Swift 3中按下创建提醒按钮时设置第二个提醒

时间:2017-05-27 12:30:18

标签: ios swift datepicker reminders

我为客户创建了一个约会提醒应用程序,但有些人表示他们希望应用程序在提前一天(24小时)和约会时提前通知他们,但是我我不确定如何编辑我的代码来执行此操作。

以下是我的工作代码,显示日期选择器上所选时间的约会:

import UIKit
import EventKit

class RemindersViewController: UIViewController {

    @IBOutlet weak var reminderText: UITextField!
    @IBOutlet weak var myDatePicker: UIDatePicker!
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
    let appDelegate = UIApplication.shared.delegate
        as! AppDelegate

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }

    @IBAction func setReminder(_ sender: AnyObject) {

        if reminderText.text == "" {

            // Create the alert controller
            let alertController = UIAlertController(title: "Information Needed", message: "Please type in your treatment and select the correct date and time you wish to be reminded about before pressing the Create Appointment Reminder button.", preferredStyle: .alert)

            // Create the actions
            let okAction = UIAlertAction(title: "Got It", style: UIAlertActionStyle.default) {
                UIAlertAction in
                NSLog("OK Pressed")
            }

            // Add the actions
            alertController.addAction(okAction)

            // Present the controller
            self.present(alertController, animated: true, completion: nil)

        } else {

            activityIndicator.startAnimating()

            let eventStore = EKEventStore()
            eventStore.requestAccess(
                to: EKEntityType.reminder, completion: {(granted, error) in
                    if !granted {
                        print("Access to store not granted")
                        print(error!.localizedDescription)
                    } else {
                        print("Access granted")
                        self.createReminder(in: eventStore)
                    }
            })
        }

        self.reminderText.resignFirstResponder()
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {

        textField.resignFirstResponder()

        return true
    }

    func createReminder(in eventStore: EKEventStore) {

        let reminder = EKReminder(eventStore: eventStore)

        reminder.title = reminderText.text! + " " + "(Pose Beauty Salon)"
        reminder.calendar =
            eventStore.defaultCalendarForNewReminders()
        let date = myDatePicker.date
        let alarm = EKAlarm(absoluteDate: date)

        reminder.addAlarm(alarm)

        do {
            try eventStore.save(reminder,
                                             commit: true)
        } catch let error  {
            print("Reminder failed with error \(error.localizedDescription)")
        }

        // Create the alert controller
        let alertController = UIAlertController(title: "Reminder Created Successfully", message: "Your \(reminderText.text!) appointment reminder at Pose Beauty Salon has been successfully created in your iPhone Reminders app.  Thank You!", preferredStyle: .alert)

        // Create the actions
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
            UIAlertAction in
            NSLog("OK Pressed")
            self.reminderText.text = ""
            self.activityIndicator.stopAnimating() 
        }

        // Add the actions
        alertController.addAction(okAction)

        // Present the controller
        self.present(alertController, animated: true, completion: nil)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        reminderText.endEditing(true)
    }
}

我已按如下方式更改了我的代码,但我只保存了一次提醒:

func createReminder(in eventStore: EKEventStore) {

    let reminder = EKReminder(eventStore: eventStore)
    let secondReminder = EKReminder(eventStore: eventStore)

    reminder.title = reminderText.text! + " " + "(Pose Beauty Salon)"
    reminder.calendar =
        eventStore.defaultCalendarForNewReminders()
    secondReminder.title = reminderText.text! + " Tomorrow " + "(Pose Beauty Salon)"
    secondReminder.calendar =
        eventStore.defaultCalendarForNewReminders()
   // let date = myDatePicker.date
    let actualDate = myDatePicker.date
    let earlyReminderDate = actualDate.addingTimeInterval(-3600*24)
    //let alarm = EKAlarm(absoluteDate: date)
    let alarm = EKAlarm(absoluteDate: actualDate)
    let secondAlarm = EKAlarm(absoluteDate: earlyReminderDate)

    reminder.addAlarm(alarm)
    secondReminder.addAlarm(secondAlarm)

2 个答案:

答案 0 :(得分:3)

似乎eventStore.defaultCalendarForNewReminders()不允许多个警报。

如果您将提醒保存到日历应用程序,则可以实现此行为。

我对您的代码进行了一些更改,希望这很有用: 更新了访问请求

let eventStore = EKEventStore()
        eventStore.requestAccess(
            to: EKEntityType.event, completion: {(granted, error) in
                if !granted {
                    print("Access to store not granted")
                    print(error!.localizedDescription)
                } else {
                    print("Access granted")
                    self.createReminder(in: eventStore)
                }
        })

创建一个EKEvent而不是EKReminder并打开EKEventEditViewController

func createReminder(in eventStore: EKEventStore) {

    let reminder = EKEvent(eventStore: eventStore)

    reminder.title = reminderText.text! + " " + "(Pose Beauty Salon)"
    reminder.calendar =
        eventStore.defaultCalendarForNewEvents
    let date = myDatePicker.date
    let alarm = EKAlarm(absoluteDate: date)
    reminder.addAlarm(alarm)

    let earlierDate = date.addingTimeInterval(-3600*24)
    let earlierAlarm = EKAlarm(absoluteDate: earlierDate)
    reminder.addAlarm(earlierAlarm)

    reminder.startDate = date
    reminder.endDate = date.addingTimeInterval(3600)

    do {
        try eventStore.save(reminder, span: .thisEvent, commit: true)
    } catch let error  {
        print("Reminder failed with error \(error.localizedDescription)")
        return
    }


    // Create the alert controller
    let alertController = UIAlertController(title: "Reminder Created Successfully", message: "Your \(reminderText.text!) appointment reminder at Pose Beauty Salon has been successfully created in your iPhone Reminders app.  Thank You!", preferredStyle: .alert)

    // Create the actions
    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
        UIAlertAction in
        NSLog("OK Pressed")
        self.reminderText.text = ""
        self.activityIndicator.stopAnimating()
    }

    // Add the actions
    alertController.addAction(okAction)

    // Present the controller
    self.present(alertController, animated: true, completion: nil)


}

EKEventEditViewDelegate

添加了委托方法
func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) {

    switch action
    {
    case .saved:
        let alertController = UIAlertController(title: "Reminder Created Successfully", message: "Your \(reminderText.text!) appointment reminder at Pose Beauty Salon has been successfully created in your iPhone Reminders app.  Thank You!", preferredStyle: .alert)

        // Create the actions
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
            UIAlertAction in
            NSLog("OK Pressed")
            self.reminderText.text = ""
            self.activityIndicator.stopAnimating()
        }

        // Add the actions
        alertController.addAction(okAction)

        // Present the controller
        self.present(alertController, animated: true, completion: nil)

    default:
        self.dismiss(animated: true, completion: nil)
    }

}

答案 1 :(得分:0)

您可以使用所有相同的数据创建两个单独的提醒,但具有不同的日期,如下所示:

let actualDate = myDatePicker.date
let earlyReminderDate = actualDate.addingTimeInterval(-3600*24)