LAST CHANCE: Save 50% on all my Swift books and bundles! >>

Get Reminders List and update View in the right way

Forums > SwiftUI

@Kdkwn  

Hello, I want that the user can choose a list from the Reminders app where some should be saved.

Now, I get the Reminder-Lists like this:

func fetchReminderLists() -> [EKCalendar] {
        let reminderLists = eventStore.calendars(for: .reminder)
        var listFromUser: [EKCalendar] = []

        for list in reminderLists {
            listFromUser.append(list)
        }

        return listFromUser
    }

In my SwiftUI View, I have a Picker where the Reminder-List should be displayed:


 @AppStorage("selected_reminder_list_id") var selectedReminderListId: String = ""
    @State private var reminderList: [EKCalendar] = []

Picker(selection: $selectedReminderListId) {
                                    if selectedReminderListId == "" {
                                        Text("No list selected")
                                        ForEach(reminderList, id: \.self) { reminderList in
                                            Text(reminderList.title)
                                                .tag(reminderList.calendarIdentifier)
                                        }
                                    } else {
                                        ForEach(reminderList, id: \.self) { reminderList in
                                            Text(reminderList.title)
                                                .tag(reminderList.calendarIdentifier)
                                        }
                                    }
                                } label: {
                                    HStack {
                                        Image(systemName: "list.bullet")
                                            .foregroundColor(Color.blue)
                                            .padding(.trailing, 4)

                                        Text("Reminder-Lists")
                                            .bold()
                                    }
                                }
.onAppear {
                    reminderList = ReminderManager.shared.fetchReminderLists()
                    var found = false

                    for i in 1...reminderList.count {
                        if reminderList[i - 1].calendarIdentifier == selectedReminderListId {
                            found = true
                            break
                        }
                    }

                    if !found {
                        selectedReminderListId = ""
                    }
                }                       

Now my question is, how can I listen to changes in the Reminders-App for example when a lists get deleted or added. Also is there a better solution for the Picker?

Thank you for your help!

3      

Have you read this page in the documentation?

2      

@Kdkwn  

Thanks, you also know how to respond to list changes, not just create or delete. The problem is that the view with this observer only updates for a newly created list in the reminders app or a deleted one, not for an edited one.

2      

You are observing for EKEventStoreChanged? Per the docs: "This notification is posted whenever changes are made to the Calendar database, including adding, removing, and changing events or reminders." (emphasis added)

2      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.