在Swift 3中按下创建提醒按钮时设置第二个提醒 [英] Set a second reminder when create reminder button pressed in Swift 3

查看:104
本文介绍了在Swift 3中按下创建提醒按钮时设置第二个提醒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为客户创建了约会提醒应用程序,但有些人说他们希望该应用程序提前一天(24小时)以及在约会时给他们一个更早的通知,但是我我不确定如何编辑代码来做到这一点.

I've created an appointment reminder app for customers but a few have said they would like the app to give them an earlier notification say one day in advance (24 hours) as well as at the time of the appointment, but I'm unsure how I can edit my code to do this.

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

Here is my working code that shows the appointment at the chosen time on the date picker:

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)
    }
}

我对代码进行了如下更改,但仅将提醒保存了一次:

I've changed my code as follows but I only get the reminder saved once:

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)

推荐答案

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

如果将提醒保存到日历应用中,则可以实现此行为.

You can achieve this behaviour if you save the reminder to the calendar app instead.

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

I made some changes to your code to do this, hopefully this is useful: Updated the access request

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

Create an EKEvent instead of EKReminder and open the 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)
    }

}

这篇关于在Swift 3中按下创建提醒按钮时设置第二个提醒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆