Skip to content

Commit 37c26c3

Browse files
authored
Поправил краш календаря (#326)
В iOS 17 нужно по-новому запрашивать доступ для добавления событий в календарь, а старый способ приводит к крашу как раз на iOS 17+. Теперь будет 2 варианта запроса доступа, пока не откажемся от iOS < 17
1 parent ae462b0 commit 37c26c3

File tree

4 files changed

+40
-32
lines changed

4 files changed

+40
-32
lines changed

SwiftUI-WorkoutApp.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@
468468
"$(inherited)",
469469
"@executable_path/Frameworks",
470470
);
471-
MARKETING_VERSION = 3.8.0;
471+
MARKETING_VERSION = 3.8.1;
472472
PRODUCT_BUNDLE_IDENTIFIER = com.FGU.WorkOut;
473473
PRODUCT_NAME = WorkoutApp;
474474
RUN_CLANG_STATIC_ANALYZER = YES;
@@ -522,7 +522,7 @@
522522
"$(inherited)",
523523
"@executable_path/Frameworks",
524524
);
525-
MARKETING_VERSION = 3.8.0;
525+
MARKETING_VERSION = 3.8.1;
526526
PRODUCT_BUNDLE_IDENTIFIER = com.FGU.WorkOut;
527527
PRODUCT_NAME = WorkoutApp;
528528
RUN_CLANG_STATIC_ANALYZER = YES;

SwiftUI-WorkoutApp/Resources/Localizable.xcstrings

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,12 +2437,12 @@
24372437
}
24382438
}
24392439
},
2440-
"Необходимо разрешить полный доступ к календарю в настройках телефона" : {
2440+
"Необходимо разрешить доступ к календарю в настройках телефона" : {
24412441
"localizations" : {
24422442
"en" : {
24432443
"stringUnit" : {
24442444
"state" : "translated",
2445-
"value" : "You need to allow full access to the calendar in your phone settings"
2445+
"value" : "You need to allow access to the calendar in your phone settings"
24462446
}
24472447
}
24482448
}
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import EventKit
1+
@preconcurrency import EventKit
22
import Foundation
3+
import SWUtils
34

45
final class CalendarManager: ObservableObject {
56
let eventStore = EKEventStore()
67
@Published var showCalendar = false
78
@Published var showSettingsAlert = false
89

910
@MainActor
10-
func requestAccess() {
11-
switch EKEventStore.authorizationStatus(for: .event) {
12-
case .fullAccess: showCalendar = true
13-
case .restricted, .denied: showSettingsAlert = true
14-
default:
15-
eventStore.requestAccess(to: .event) { [weak self] granted, _ in
16-
DispatchQueue.main.async {
17-
self?.showCalendar = granted
18-
self?.showSettingsAlert = !granted
19-
}
20-
}
11+
func requestAccess() async throws {
12+
let grantedAccess = if #available(iOS 17.0, *) {
13+
try await eventStore.requestWriteOnlyAccessToEvents()
14+
} else {
15+
try await eventStore.requestAccess(to: .event)
16+
}
17+
if grantedAccess {
18+
showCalendar = true
19+
} else {
20+
showSettingsAlert = true
2121
}
2222
}
2323
}

SwiftUI-WorkoutApp/Screens/Events/EventDetailsScreen.swift

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -172,24 +172,32 @@ private extension EventDetailsScreen {
172172
}
173173

174174
var addToCalendarButton: some View {
175-
Button("Добавить в календарь", action: calendarManager.requestAccess)
176-
.buttonStyle(SWButtonStyle(mode: .tinted, size: .large))
177-
.padding(.top, 12)
178-
.sheet(isPresented: $calendarManager.showCalendar) {
179-
EKEventEditViewControllerRepresentable(
180-
eventStore: calendarManager.eventStore,
181-
event: event
182-
)
183-
}
184-
.alert(
185-
"Необходимо разрешить полный доступ к календарю в настройках телефона",
186-
isPresented: $calendarManager.showSettingsAlert
187-
) {
188-
Button("Отмена", role: .cancel) {}
189-
Button("Перейти") {
190-
URLOpener.open(URL(string: UIApplication.openSettingsURLString))
175+
Button("Добавить в календарь") {
176+
Task {
177+
do {
178+
try await calendarManager.requestAccess()
179+
} catch {
180+
SWAlert.shared.presentDefaultUIKit(error)
191181
}
192182
}
183+
}
184+
.buttonStyle(SWButtonStyle(mode: .tinted, size: .large))
185+
.padding(.top, 12)
186+
.sheet(isPresented: $calendarManager.showCalendar) {
187+
EKEventEditViewControllerRepresentable(
188+
eventStore: calendarManager.eventStore,
189+
event: event
190+
)
191+
}
192+
.alert(
193+
"Необходимо разрешить доступ к календарю в настройках телефона",
194+
isPresented: $calendarManager.showSettingsAlert
195+
) {
196+
Button("Отмена", role: .cancel) {}
197+
Button("Перейти") {
198+
URLOpener.open(URL(string: UIApplication.openSettingsURLString))
199+
}
200+
}
193201
}
194202

195203
var descriptionSection: some View {

0 commit comments

Comments
 (0)