Отказ в доступе к Календарю по-прежнему позволяет предоставить доступ к календарю приложению.IOS

Программируем под IOS
Ответить Пред. темаСлед. тема
Anonymous
 Отказ в доступе к Календарю по-прежнему позволяет предоставить доступ к календарю приложению.

Сообщение Anonymous »


I am trying Event Kit with SwiftUI and when I tap on a button within the app, my app checks for calendar authorization and if not authorized, it shows a prompt to the user. If the user denies access to calendar, iOS 16.4 doesn't show create event view, but iOS 17.0 does.

How can I avoid iOS 17 from displaying create event view after user denies access to the calendar?

Code:

import SwiftUI import Foundation import EventKitUI struct ContentView: View { @State private var openEventView: Bool = false var body: some View { NavigationStack { VStack { Button { self.openEventView = true } label: { HStack { Spacer() Image(systemName: "calendar.badge.plus").font(.body.bold()).foregroundColor(Color.blue).padding(.trailing, 5.0) Text("Add to Calendar").font(.body.bold()).foregroundColor(Color.blue) Spacer() } .frame(maxWidth: .infinity) .contentShape(Rectangle()) } .frame(width: UIScreen.main.bounds.width - 40.0, height: 44.0) .background(Color.blue.opacity(0.5)) .cornerRadius(10.0) .contentShape(Rectangle()) .listRowBackground(EmptyView()) .listRowInsets(EdgeInsets()) } .sheet(isPresented: self.$openEventView, content: { EventEditView() }) } } } struct EventEditView: UIViewControllerRepresentable { func makeCoordinator() -> Coordinator { return Coordinator(self) } @Environment(\.presentationMode) var presentationMode @State private var eventStoreInitAfterAuth = false @State private var eventStore: EKEventStore = EKEventStore() let event: EKEvent? = nil func makeUIViewController(context: UIViewControllerRepresentableContext) -> EKEventEditViewController { Task { await checkCalendarAuthorizationStatus() } let eventEditViewController = EKEventEditViewController() eventEditViewController.eventStore = eventStore if let event = event { eventEditViewController.event = event } eventEditViewController.editViewDelegate = context.coordinator return eventEditViewController } func updateUIViewController(_ uiViewController: EKEventEditViewController, context: UIViewControllerRepresentableContext) { } class Coordinator: NSObject, EKEventEditViewDelegate { let parent: EventEditView init(_ parent: EventEditView) { self.parent = parent } func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) { parent.presentationMode.wrappedValue.dismiss() if action != .canceled { // Do something } } } func checkCalendarAuthorizationStatus() async -> Bool { let status = EKEventStore.authorizationStatus(for: .event) switch status { case .notDetermined: return await requestAccessToCalendar() case .authorized: if !eventStoreInitAfterAuth { eventStore = EKEventStore() } eventStoreInitAfterAuth = true return true case .restricted, .denied: return false default: return false } } private func requestAccessToCalendar() async -> Bool { do { if #available(iOS 17.0, *) { let accessGranted = try await eventStore.requestFullAccessToEvents() return accessGranted } else { let accessGranted = try await eventStore.requestAccess(to: .event) return accessGranted } } catch { return false } } } Note: To successfully run my code, you will have to add "NSCalendarsUsageDescription" description to info plist file otherwise app will crash.

First image is from iOS 16.4 and second image is from iOS 17.0 after I deny access to Calendar access.

I would actually like to close the sheet if user denies access to calendar. What am I doing wrong in the code?


Изображение

Изображение



Источник: https://stackoverflow.com/questions/780 ... to-the-app
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «IOS»