Просмотр исходного кода

Change permissions required for calendar in iOS 17+ (#234)

* Change permissions required for calendar in iOS 17+
Andreas Stokholm 2 лет назад
Родитель
Сommit
533aaaf0fc

+ 4 - 0
FreeAPS/Resources/Info.plist

@@ -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>

+ 13 - 0
FreeAPS/Sources/Modules/CGM/View/CGMRootView.swift

@@ -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)
+                                    }
+                                }
+                            }
                         }
                     }
 

+ 42 - 5
FreeAPS/Sources/Services/Calendar/CalendarManager.swift

@@ -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))