将数据(标签和日期选择器)从我的应用程序发送到提醒应用程序

Sending data (label and date picker) from my app to the Reminders app

我在我的应用程序中创建了一个约会提醒部分,但应用程序的首次使用似乎没有被存储?当我单击“创建提醒”按钮时,我收到弹出式警报,提示已成功创建,然后是“想要访问您的提醒”弹出式窗口。因为这个人的第一次约会没有被存储,我不知道我做错了什么?

这是我的代码:

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

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

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