iosswifteventkitreminders

How to create reminders that don't have to be shown in the Apple's Reminders app


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.


Solution

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