I’m making an attempt Occasion Equipment with SwiftUI and after I faucet on a button throughout the app, my app checks for calendar authorization and if not approved, it reveals a immediate to the consumer. If the consumer denies entry to calendar, iOS 16.4 would not present create occasion view, however iOS 17.0 does.
How can I keep away from iOS 17 from displaying create occasion view after consumer denies entry to the calendar?
Code:
import SwiftUI
import Basis
import EventKitUI
struct ContentView: View {
@State non-public var openEventView: Bool = false
var physique: some View {
NavigationStack {
VStack {
Button { self.openEventView = true } label: {
HStack {
Spacer()
Picture(systemName: "calendar.badge.plus").font(.physique.daring()).foregroundColor(Coloration.blue).padding(.trailing, 5.0)
Textual content("Add to Calendar").font(.physique.daring()).foregroundColor(Coloration.blue)
Spacer()
}
.body(maxWidth: .infinity)
.contentShape(Rectangle())
}
.body(width: UIScreen.foremost.bounds.width - 40.0, top: 44.0)
.background(Coloration.blue.opacity(0.5))
.cornerRadius(10.0)
.contentShape(Rectangle())
.listRowBackground(EmptyView())
.listRowInsets(EdgeInsets())
}
.sheet(isPresented: self.$openEventView, content material: {
EventEditView()
})
}
}
}
struct EventEditView: UIViewControllerRepresentable {
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
@Surroundings(.presentationMode) var presentationMode
@State non-public var eventStoreInitAfterAuth = false
@State non-public var eventStore: EKEventStore = EKEventStore()
let occasion: EKEvent? = nil
func makeUIViewController(context: UIViewControllerRepresentableContext<EventEditView>) -> EKEventEditViewController {
Process {
await checkCalendarAuthorizationStatus()
}
let eventEditViewController = EKEventEditViewController()
eventEditViewController.eventStore = eventStore
if let occasion = occasion {
eventEditViewController.occasion = occasion
}
eventEditViewController.editViewDelegate = context.coordinator
return eventEditViewController
}
func updateUIViewController(_ uiViewController: EKEventEditViewController, context: UIViewControllerRepresentableContext<EventEditView>) {
}
class Coordinator: NSObject, EKEventEditViewDelegate {
let mum or dad: EventEditView
init(_ mum or dad: EventEditView) {
self.mum or dad = mum or dad
}
func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith motion: EKEventEditViewAction) {
mum or dad.presentationMode.wrappedValue.dismiss()
if motion != .canceled {
// Do one thing
}
}
}
func checkCalendarAuthorizationStatus() async -> Bool {
let standing = EKEventStore.authorizationStatus(for: .occasion)
swap standing {
case .notDetermined:
return await requestAccessToCalendar()
case .approved, .fullAccess:
if !eventStoreInitAfterAuth {
eventStore = EKEventStore()
}
eventStoreInitAfterAuth = true
return true
case .restricted, .denied:
return false
default:
return false
}
}
non-public func requestAccessToCalendar() async -> Bool {
do {
if #out there(iOS 17.0, *) {
let accessGranted = attempt await eventStore.requestFullAccessToEvents()
return accessGranted
} else {
let accessGranted = attempt await eventStore.requestAccess(to: .occasion)
return accessGranted
}
} catch {
return false
}
}
}
Notice: To efficiently run my code, you’ll have to add “NSCalendarsUsageDescription” description to information plist file in any other case app will crash.
First picture is from iOS 16.4 and second picture is from iOS 17.0 after I deny entry to Calendar entry.
I’d really like to shut the sheet if consumer denies entry to calendar. What am I doing mistaken within the code?