I am switching on EKAuthorizationStatus
but even after requestAuthorisation(to:commit:)
is called and returned true and no error the switch statement still matches the .notDetermined
case and a recursion in it is producing an infinite loop. And it drives me nuts!
I tried to find out how requestAuthorisation(to:commit:)
actually works since I have the feeling this problem is all about concurrency or something but I couldn't find anything, so I am having trouble truly reason about the situation.
And since the recursion in my code is definitely part of this infinite loop I tried an approach without recursions.
But since the EKAuthorizationStatus
could change in between my app's calls to the event store I want to check it beforehand reacting to all it's states accordingly. And so I would have to call my methods for switching over the authorization status and one for requesting it and handle any errors all over my class which I don't want to for reasons of readability, safety and sanity.
private func confirmAuthorization(for entityType: EKEntityType) throws {
switch EKEventStore.authorizationStatus(for: entityType) {
case EKAuthorizationStatus.notDetermined:
// Request authorisation for the entity type.
requestAuthorisation(for: entityType)
// Switch again.
try confirmAuthorization(for: entityType)
case EKAuthorizationStatus.denied:
print("Access to the event store was denied.")
throw EventHelperError.authorisationDenied
case EKAuthorizationStatus.restricted:
print("Access to the event store was restricted.")
throw EventHelperError.authorisationRestricted
case EKAuthorizationStatus.authorized:
print("Acces to the event store granted.")
}
}
private func requestAuthorisation(for entityType: EKEntityType) {
store.requestAccess(to: entityType) { (granted, error) in
if (granted) && (error == nil) {
DispatchQueue.main.async {
print("User has granted access to \(String(describing: entityType))") // It's being printed over and over
}
} else {
DispatchQueue.main.async {
print("User has denied access to \(String(describing: entityType))")
}
}
}
}
I expected the switch would match to the .notDetermined
case on first launch, where it would request the authorization. So when I switch through the status again it should now match a different case like .authorized
or .denied
.
But actually it matches the .notDetermined
case again and the access gets granted over and over. \ >:[
console:
>2019-01-08 12:50:51.314628+0100 EventManager[4452:190572] libMobileGestalt MobileGestalt.c:890: MGIsDeviceOneOfType is not supported on this platform.
>2019-01-08 12:50:54.608391+0100 EventManager[4452:190572] Adding a new event.
>2019-01-08 12:50:54.784684+0100 EventManager[4452:190572] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/***/Library/Developer/CoreSimulator/Devices/********-****-****-****-************/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
>2019-01-08 12:50:54.785638+0100 EventManager[4452:190572] [MC] Reading from private effective user settings.
>Acces to the event store granted.
>Saved event with identifier: Optional("F8EAC467-9EC2-476C-BF30-45588240A8D0:903EF489-BB52-4A86-917B-DF72494DEA3D")
>2019-01-08 12:51:03.019751+0100 EventManager[4452:190572] Events succsessfully saved.
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>[…]
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>2019-01-08 12:51:03.291606+0100 EventManager[4452:190572] [Common] _BSMachError: port 26b03; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"
>2019-01-08 12:51:03.317800+0100 EventManager[4452:190572] [Common] _BSMachError: port 26b03; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>[…]
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>Acces to the event store granted.
>Preset <EventManager.EventCreationPreset: 0x6000020ca340> needs update.
>Acces to the event store granted.
>Preset <EventManager.EventCreationPreset: 0x6000020ca340> was updated.
>2019-01-08 12:51:03.567071+0100 EventManager[4452:190572] Events succsessfully saved.
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>User has granted access to EKEntityType
>[…]