簡體   English   中英

請求 EventKit 訪問 macOS SwiftUI

[英]Requesting EventKit access macOS SwiftUI

我正在嘗試請求訪問 SwiftUI MacOS 應用程序中的日歷,但它立即拒絕了它。

下面是一些可測試的代碼,突出顯示了我想要實現的目標。 非常感謝。

在我之前在 MacOS 上請求私人信息的經驗中,應該出現通知而不是 iOS 上的彈出窗口。

我仍在嘗試為 SwiftUI 找出 EventKit,所以請原諒任何錯誤。

struct ContentView: View {
    
    let eventStore = EKEventStore()
    
    var body: some View {
        
        Text("Test")
            .onAppear{
                checkCalendarAuthorizationStatus()
            }
        
    }
    
    
    func checkCalendarAuthorizationStatus() {

        
        switch EKEventStore.authorizationStatus(for: .event) {
        case .authorized:
            insertEvent(store: eventStore)
            case .denied:
                print("Access denied")
            case .notDetermined:
            // 3
                eventStore.requestAccess(to: .event, completion:
                  {(granted: Bool, error: Error?) -> Void in
                      if granted {
                        self.insertEvent(store: eventStore)
                      } else {
                            print("Access denied")
                      }
                })
            default:
                print("Case default")
        }
    }
    
    func insertEvent(store: EKEventStore) {
        // 1
        let calendars = store.calendars(for: .event)
            
        for calendar in calendars {
            // 2
            if calendar.title == "Calendar" {
                // 3
                let startDate = Date()
                // 2 hours
                let endDate = startDate.addingTimeInterval(2 * 60 * 60)
                    
                // 4
                let event = EKEvent(eventStore: store)
                event.calendar = calendar
                    
                event.title = "New Meeting"
                event.startDate = startDate
                event.endDate = endDate
                    
                // 5
                do {
                    try store.save(event, span: .thisEvent)
                }
                catch {
                   print("Error saving event in calendar")             }
                }
        }
    }
}

您不應該檢查狀態,而是使用 EKEventStore().requestAccess(... 直接請求訪問,如文檔所述

/**
    @method     requestAccessToEntityType:completion:
    @discussion Users are able to grant or deny access to event and reminder data on a per-app basis. To request access to
                event and/or reminder data, call -requestAccessToEntityType:completion:. This will not block the app while
                the user is being asked to grant or deny access.
 
                Until access has been granted for an entity type, the event store will not contain any calendars for that
                entity type, and any attempt to save will fail. The user will only be prompted the first time access is
                requested; any subsequent instantiations of EKEventStore will use the existing permissions. When the user
                taps to grant or deny access, the completion handler will be called on an arbitrary queue.
*/
@available(macOS 10.9, *)
open func requestAccess(to entityType: EKEntityType, completion: @escaping EKEventStoreRequestAccessCompletionHandler)

所以使用以下

func checkCalendarAuthorizationStatus() {

    eventStore.requestAccess(to: .event, completion:
      {(granted: Bool, error: Error?) -> Void in
          if granted {
            self.insertEvent(store: eventStore)
          } else {
            print("Access denied")
          }
    })
}

並確保在Info.plist中添加了NSCalendarsUsageDescription描述。

備份

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM