无法请求访问 Swift 4.2 中 IOS 日历的权限

问题描述 投票:0回答:3

我已经尝试过之前请求许可将项目添加到 IOS 日历的示例。它们不适用于 Xcode 10.1 (Swift 4.2)。当我尝试编译代码时,出现错误。如果我注释掉以“EKEventstore.requestAccess”开头的行,则代码将执行“.not.Determined”循环。

错误是“实例成员‘requestAccess’不能在类型‘EKEventStore’上使用;您是否打算改用此类型的值?”

任何人都可以找到我的错误,以便 IOS 应用程序有权将事件添加到日历吗?

func SOGetPermissionCalendarAccess() {

    switch EKEventStore.authorizationStatus(for: .event) {

    case .authorized:
        print("Authorized")

    case .denied:
        print("Access denied")

    case .notDetermined:
        EKEventStore.requestAccess(to: .event, completion:
            {[weak self] (granted: Bool, error: Error?) -> Void in
                if granted {
                    print("Access granted")
                } else {
                    print("Access denied")
                }
        })

        print("Not Determined")
    default:
        print("Case Default")
        }

}
ios iphone swift4 xcode10 ios12
3个回答
8
投票

您应该创建事件存储实例,如下所示,

let eventStore = EKEventStore()

之后您可以像下面这样提出权限请求,

switch EKEventStore.authorizationStatus(for: .event) {

        case .authorized:
            print("Authorized")

        case .denied:
            print("Access denied")

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

            print("Not Determined")
        default:
            print("Case Default")
        }

请参阅此链接了解更多信息。


0
投票

EKEventStore 的 Apple 文档是执行 reset() 方法,这也没有帮助。 我的解决方法是在 requestAccess 方法之后再次初始化 EKEventStore。

private var store: EKEventStore
private var calendars: [EKCalendar] = []

private func requestAccessCalendars() {
    store.requestAccess(to: .event) { [weak self] (accessGranted, error) in
        if accessGranted {
            self?.store = EKEventStore() // <- second instance
            self?.store.refreshSourcesIfNecessary()

            self?.loadCalendars()
        }
    }
}

private func loadCalendars() {
    let cals = store.calendars(for: .event)
    for c in cals {
        if c.allowsContentModifications { // without birthdays, holidays etc'...
            calendars.append(c)
        }
    }
}

0
投票

iOS17 中,request(to:,completion:) 方法已被弃用,并带有 requestFullAccessToEvents(completion:)requestWriteOnlyAccessToEvents(completion:) .

https://developer.apple.com/documentation/eventkit/ekeventstore/4162272-requestfullaccesstoevents

因此请求的更新版本可能如下;

switch EKEventStore.authorizationStatus(for: .event) {
    case .fullAccess:
        print("Full Access")
    case .authorized:
        print("Authorized")
    case .writeOnly:
        print("Write Only Access")
    case .denied:
        print("Authorization Denied")
    case .notDetermined:
        if #available(iOS 17.0, *) {
            eventStore.requestFullAccessToEvents { isGranted, error in
                print("isGranted: ", isGranted)
            }
        } else {
            eventStore.requestAccess(to: .event) { isGranted, error in
                print("isGranted: ", isGranted)
            }
        }
    default:
        print("Default Case")
}
© www.soinside.com 2019 - 2024. All rights reserved.