我已经在我的应用程序中创建了一个约会提醒部分,但似乎第一次使用该应用程序没有被存储?当我单击创建提醒按钮时,我收到弹出警报,说它已成功创建,然后是想要访问您的提醒弹出窗口。正因为如此,人们的第一次约会没有被存储,我无法弄清楚我做错了什么?
这是我的代码:
import UIKit
import EventKit
class FirstViewController: UIViewController {
@IBOutlet weak var reminderText: UITextField!
@IBOutlet weak var myDatePicker: UIDatePicker!
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 {
if appDelegate.eventStore == nil {
appDelegate.eventStore = EKEventStore()
appDelegate.eventStore?.requestAccess(
to: EKEntityType.reminder, completion: {(granted, error) in
if !granted {
print("Access to store not granted")
print(error!.localizedDescription)
} else {
print("Access granted")
}
})
}
if (appDelegate.eventStore != nil) {
self.createReminder()
}
}
self.reminderText.resignFirstResponder()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func createReminder() {
let reminder = EKReminder(eventStore: appDelegate.eventStore!)
reminder.title = reminderText.text! + " " + "(Pose Beauty Salon)"
reminder.calendar =
appDelegate.eventStore!.defaultCalendarForNewReminders()
let date = myDatePicker.date
let alarm = EKAlarm(absoluteDate: date)
reminder.addAlarm(alarm)
do {
try appDelegate.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")
}
// Add the actions
alertController.addAction(okAction)
// Present the controller
self.present(alertController, animated: true, completion: nil)
reminderText.text = ""
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
reminderText.endEditing(true)
}
}
你的逻辑比它需要的要复杂得多。 您可以利用这样一个事实,即在授予(或拒绝)访问权限后请求访问事件存储时,将在不提示用户的情况下使用这些权限。
从其他类初始化 AppDelegate 的属性也是糟糕的形式。 您可能甚至不需要 AppDelegate 上的 EventStore - 您可以在需要时创建一个实例。
您可以使用以下内容:
} else {
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)
}
})
}
func createReminder(in eventStore: EKEventStore) {
....
}