在 Swift 中按下创建提醒按钮时设置第二个提醒 3
Set a second reminder when create reminder button pressed in Swift 3
我已经为客户创建了一个约会提醒应用程序,但一些人表示他们希望该应用程序能够提前一天(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)
您可以使用所有相同的数据创建两个单独的提醒,但日期不同,如下所示:
let actualDate = myDatePicker.date
let earlyReminderDate = actualDate.addingTimeInterval(-3600*24)
似乎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)
}
}
我已经为客户创建了一个约会提醒应用程序,但一些人表示他们希望该应用程序能够提前一天(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)
您可以使用所有相同的数据创建两个单独的提醒,但日期不同,如下所示:
let actualDate = myDatePicker.date
let earlyReminderDate = actualDate.addingTimeInterval(-3600*24)
似乎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)
}
}