-4

With the following code I can successfully create a reminder event and add an alarm to it that triggers 10 seconds after the event has been created. What I don't like about the way the reminder is created is that it shows in the Apple's Reminders app and when you get the notification message in your device, it shows the Reminders' app icon.

Is it possible to make the reminder private so it doesn't show in Apple's Reminders app? If not, what are my options to achieve such of task?

Note: I don't mind storing the reminders in the standard reminders local database as long as they don't show in the default Reminders app.

import EventKit

class ViewController: UIViewController{
    var eventStore = EKEventStore()

    override func viewDidLoad(){
        super.viewDidLoad()
        // get user permission
        eventStore.requestAccess(to: EKEntityType.reminder, completion: {(granted, error) in
            if !granted{
                print("Access denied!")
            }
        })
    }

    @IBAction func createReminder(_ sender: Any) {
        let reminder = EKReminder(eventStore: self.eventStore)
        reminder.title = "Get Milk from the Store"
        reminder.calendar = eventStore.defaultCalendarForNewReminders()
        let date = Date()
        let alarm = AKAlarm (absoluteDate: date.addingTimeInterval(10) as Date)
        reminder.addAlarm(alarm)

        do {
            try eventStore.save(reminder, commit: true)
        } catch let error {
            print("Error: \(error.localizedDescription)")
        }
    }
}

FYI - To make the above code work you would need to add the NSRemindersUsageDescription key in the info.plist file.

Benny Bottema
  • 11,111
  • 10
  • 71
  • 96
fs_tigre
  • 10,650
  • 13
  • 73
  • 146
  • 1
    Are you asking how to write a reminder app that has nothing to do with Apple's EventKit? – matt Jan 06 '20 at 04:54
  • Yes. But it’s not thatI don’t want to use EventKit, I just don’t want the reminders to be shown in the Reminders app and I I think is what EventKit used for. – fs_tigre Jan 06 '20 at 11:33

1 Answers1

0

Just for the record, what I was looking for was User Notifications.

Here is a complete example.

User Notifications

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
        if granted{
            print("User gave permissions for local notifications")
        }else{
            print("User did NOT give permissions for local notifications")
        }
    }
    return true
}


 override func viewDidLoad() {
    super.viewDidLoad()
    setReminderAtTime()
} 

func setReminderAtTime(){
    let reminderTime:TimeInterval = 60

    let center = UNUserNotificationCenter.current()
    center.removeAllPendingNotificationRequests()
    
    let content =  UNMutableNotificationContent()
    content.title = "Title"
    content.body = "Notification Message!."
    content.sound = .default
    
    let trigger =  UNTimeIntervalNotificationTrigger(timeInterval: reminderTime, repeats: false)
    let request = UNNotificationRequest(identifier: "reminderName", content: content, trigger: trigger)
    
    center.add(request) { (error) in
        if error != nil{
            print("Error = \(error?.localizedDescription ?? "error local notification")")
        }
    }
}
fs_tigre
  • 10,650
  • 13
  • 73
  • 146