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

How*_*tis 4 iphone ios swift4 ios12 xcode10

我已经尝试了要求向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")
        }

}
Run Code Online (Sandbox Code Playgroud)

Nat*_*jan 5

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

let eventStore = EKEventStore()
Run Code Online (Sandbox Code Playgroud)

之后,您可以如下所示进行许可请求,

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")
        }
Run Code Online (Sandbox Code Playgroud)

请参考此链接以获取更多信息。