Skip to content

Commit

Permalink
Change permissions required for calendar in iOS 17+ (Artificial-Pancr…
Browse files Browse the repository at this point in the history
…eas#234)

* Change permissions required for calendar in iOS 17+
  • Loading branch information
AndreasStokholm authored and mountrcg committed Oct 6, 2023
1 parent 17696c4 commit 76b73e2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
4 changes: 4 additions & 0 deletions FreeAPS/Resources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
<key>NSCalendarsFullAccessUsageDescription</key>
<string>To create events with BG reading values, so that they can be viewed on Apple Watch and CarPlay</string>
<key>LSApplicationCategoryType</key>
<string></string>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
Expand Down
13 changes: 13 additions & 0 deletions FreeAPS/Sources/Modules/CGM/View/CGMRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ extension CGM {
}
Toggle("Display Emojis as Labels", isOn: $state.displayCalendarEmojis)
Toggle("Display IOB and COB", isOn: $state.displayCalendarIOBandCOB)
} else if state.createCalendarEvents {
if #available(iOS 17.0, *) {
Text(
"If you are not seeing calendars to choose here, please go to Settings -> iAPS -> Calendars and change permissions to \"Full Access\""
).font(.footnote)

Button("Open Settings") {
// Get the settings URL and open it
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url)
}
}
}
}
}

Expand Down
47 changes: 42 additions & 5 deletions FreeAPS/Sources/Services/Calendar/CalendarManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,54 @@ final class BaseCalendarManager: CalendarManager, Injectable {
let status = EKEventStore.authorizationStatus(for: .event)
switch status {
case .notDetermined:
EKEventStore().requestAccess(to: .event) { granted, error in
if let error = error {
warning(.service, "Calendar access not granded", error: error)
#if swift(>=5.9)
if #available(iOS 17.0, *) {
EKEventStore().requestFullAccessToEvents(completion: { (granted: Bool, error: Error?) -> Void in
if let error = error {
warning(.service, "Calendar access not granted", error: error)
}
promise(.success(granted))
})
} else {
EKEventStore().requestAccess(to: .event) { granted, error in
if let error = error {
warning(.service, "Calendar access not granted", error: error)
}
promise(.success(granted))
}
}
promise(.success(granted))
}
#else
EKEventStore().requestAccess(to: .event) { granted, error in
if let error = error {
warning(.service, "Calendar access not granted", error: error)
}
promise(.success(granted))
}
#endif
case .denied,
.restricted:
promise(.success(false))
case .authorized:
promise(.success(true))

#if swift(>=5.9)
case .fullAccess:
promise(.success(true))
#endif

case .writeOnly:
#if swift(>=5.9)
if #available(iOS 17.0, *) {
EKEventStore().requestFullAccessToEvents(completion: { (granted: Bool, error: Error?) -> Void in
if let error = error {
print("Calendar access not upgraded")
warning(.service, "Calendar access not upgraded", error: error)
}
promise(.success(granted))
})
}
#endif

@unknown default:
warning(.service, "Unknown calendar access status")
promise(.success(false))
Expand Down

0 comments on commit 76b73e2

Please sign in to comment.