Giter Club home page Giter Club logo

Comments (14)

nemecek-filip avatar nemecek-filip commented on July 21, 2024

Hi, thanks for pointing this out. The issue with clean simulator appears to be related to Calendar not being yet initialized. Someone added info to README about this which I just merged.

Thanks for the clarification, I will look into it. Or if you want, you can try to fix the SwiftUI example and open PR :-)

from ekeventkit.example.

skegget avatar skegget commented on July 21, 2024

I've spent quite some time on the errors but I can't seem to find a solution, or even a source. I will continue digging but it would be great if you can take a look as well. I don't trust my SwiftUI skills yet 😀

For some reason I think the errors and the empty calendar list are related. As I mentioned, if I run the app on my daily driver device I still get the errors and an empty calendar list the first time the app is started after a clean install. On this device the calendar is definitely initiated.

Is almost like the available calendars are not refreshed after you accept calendar access.

from ekeventkit.example.

skegget avatar skegget commented on July 21, 2024

One step forward, two steps back.
I did some experiments and I might have found something that can be of help.

In the EventsRepository.swift, if you change let eventStore = EKEventStore() to var eventStore = EKEventStore() and then change the requestAccess function to:

func requestAccess(onGranted: @escaping Action, onDenied: @escaping Action) {
        eventStore.requestAccess(to: .event) { (granted, error) in
            if granted {
                self.eventStore = EKEventStore()
                onGranted()
            } else {
                onDenied()
            }
        }
    }

By doing this the calendar list is populated directly. However, this causes a bunch of other errors, like a EKEvent duplicate key error if you add an event. As mentioned, I was experimenting but it might be worth taking a closer look.

from ekeventkit.example.

nemecek-filip avatar nemecek-filip commented on July 21, 2024

Thanks. I will try to look into it, but I havent done much of SwiftUI since creating this update..

from ekeventkit.example.

skegget avatar skegget commented on July 21, 2024

Sorry for hammering you with posts and information, but here is a SO post with the exact same issue. The possible solution seems to be to "Make sure you aren't initializing EKEventStore before requesting calendar permissions."

https://stackoverflow.com/questions/58873603/ekeventstore-calendars-is-always-empty-after-clean-install

from ekeventkit.example.

nemecek-filip avatar nemecek-filip commented on July 21, 2024

Thanks for the search. However you need an instance of EKEventStore to request permissions 🤔 You said UIKit version is working fine? Since there the EKEventStore is created immediately by the view controller.

from ekeventkit.example.

skegget avatar skegget commented on July 21, 2024

Hm, weird . I have not tested the UIKit version.

I also found this in apples documentation on EKEventStore

"If your app has never requested access before, you must request access to events or reminders before attempting to fetch or create them. If you request data before prompting the user for access with this method, you’ll need to reset the event store with the reset() method in order to start receiving data after the user grants access."

https://developer.apple.com/documentation/eventkit/ekeventstore/1507547-requestaccess

Something like eventStore.reset(), I guess. But I'm not sure where to reset the event store?

from ekeventkit.example.

nemecek-filip avatar nemecek-filip commented on July 21, 2024

Based on the docs it makes sense to call reset before the onGranted closure is called in the requestAccess method. I dont have access to my Mac right now so I cannot try this.

from ekeventkit.example.

skegget avatar skegget commented on July 21, 2024

Unfortunately no luck. I'm really struggling with this, so if you could take a look I would really appreciate it. This is basically the last piece of my app puzzle :)

from ekeventkit.example.

nemecek-filip avatar nemecek-filip commented on July 21, 2024

I will try to fix this issue 👍

from ekeventkit.example.

skegget avatar skegget commented on July 21, 2024

Great! I've experimented with another thing and this might be the definition of bad coding, I don't know :) But it seems to work so maybe you can take some inspiration from it.

I added the requestAccess function to the top of init(), like this:

init() {
        requestAccess(onGranted: {
            self.eventStore = EKEventStore()
        }) {
           
        }
...
}

So when we gain access I'm getting a new eventStore instance. If this is usable, I don't know if you need to requestAccess in the loadEvents function?

Also, I came across authorizationStatus(for:) which seems to be used as a check on which you can act on the current status (authorized, denied, notDetermined, restricted).
https://developer.apple.com/documentation/eventkit/ekeventstore/1507239-authorizationstatus

from ekeventkit.example.

nemecek-filip avatar nemecek-filip commented on July 21, 2024

Think I found it:

selectedCalendars = loadSelectedCalendars() ?? Set([eventStore.defaultCalendarForNewEvents].compactMap({ $0 }))

This line was likely responsible for accessing eventStore before permissions were available and thus causing the issues. If you remove the ?? part, then it works.

I have updated the repo with this fix:

selectedCalendars = loadSelectedCalendars()
        
if selectedCalendars == nil {
    if EKEventStore.authorizationStatus(for: .event) == .authorized {
        selectedCalendars = Set([eventStore.defaultCalendarForNewEvents].compactMap({ $0 }))
    }
}

from ekeventkit.example.

skegget avatar skegget commented on July 21, 2024

Amazing Filip, just tested the fix and it seems to work perfectly!

I also noticed that the errors I mentioned in my first post seems to be gone as well :)

Thank you and great job!

from ekeventkit.example.

nemecek-filip avatar nemecek-filip commented on July 21, 2024

Awesome! Thanks for help fixing this :-)

from ekeventkit.example.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.