从我的应用程序向提醒应用程序

时间:2017-05-13 22:01:25

标签: ios swift eventkit reminders

我已在我的应用中创建了约会提醒部分,但似乎第一次使用该应用并未存储?当我点击创建提醒按钮时,我会收到弹出提示,说明它已成功创建,然后想要访问提醒弹出窗口。因为这个人的第一次约会没有被存储,我无法弄清楚我做错了什么?

这是我的代码:

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

1 个答案:

答案 0 :(得分:1)

你的逻辑比它需要的要复杂得多。您可以利用以下事实:在您已授予(或拒绝)访问权限后请求访问事件存储时,将使用这些权限而不提示用户。

从其他类初始化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) {
    ....
 }