【问题标题】:Unable to request permission to access the IOS Calendar in Swift 4.2无法在 Swift 4.2 中请求访问 IOS 日历的权限
【发布时间】:2019-04-24 18:59:42
【问题描述】:

我已经尝试过请求允许将项目添加到 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


    【解决方案1】:

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

    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")
            }
    

    更多信息请参考link

    【讨论】:

    • 这行得通。 “eventStore..”之后的行应该是“ {(granted: Bool, error: Error?) -> Void in”,因为没有使用 self。
    • 是的,正确。如果您没有在该完成块中使用“self”,则可以删除该部分。
    • 是否有可能以编程方式更改 macOS 设置中用户拒绝的授权?或者将应用授权重置为状态 .notDetermined?
    【解决方案2】:

    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)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      • 2020-05-08
      • 1970-01-01
      • 2019-11-28
      • 2015-07-30
      相关资源
      最近更新 更多