如果日历访问被拒绝,则显示警报Swift 3

时间:2016-12-20 16:53:17

标签: ios swift calendar swift3 alert

如果用户拒绝访问日历,我会尝试弹出“转到设置”提醒。一旦用户拒绝日历并重新打开应用程序,它就会起作用。但是我需要在用户拒绝访问后立即弹出警报。

这是我的日历访问代码:

/**
 */
func requestDeniedAlert() -> UIAlertController {
let requestAlertController = UIAlertController(title: "Calendar Access Denied", message: "Please Allow Calendar Access In Settings", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Go To Settings", style: .default){(action) in
    print("The User Will Be Taken To the Settings")
    let goToSettings = URL(string: UIApplicationOpenSettingsURLString)
    UIApplication.shared.open(goToSettings!)
}
requestAlertController.addAction(settingsAction)

return requestAlertController
}

/**
 reuestCalendarPermissions:
 @param
 @return
 */
func requestCalendarPermissions() {
eventInstance.requestAccess(to: .event, completion: {(accessGranted: Bool, error: Error?) in

    if accessGranted == true {
        print("Access Has Been Granted")
        MainScreenTableView.flagVariable.statusFlag = true
    }
    else {
        print("Change Settings to Allow Access")
        MainScreenTableView.flagVariable.statusFlag = false
    }
})
}

/**
 checkStatus:
 Notes: This function checks if the User has given calendar access to app
 If access is restricted or denied, a "Go To Settings" view will appear
 */
func checkStatus() {
let currentStatus = EKEventStore.authorizationStatus(for: EKEntityType.event)

if currentStatus == EKAuthorizationStatus.notDetermined {
    requestCalendarPermissions()
}
else if currentStatus == EKAuthorizationStatus.authorized {
    print("Access Has Been Granted")
    MainScreenTableView.flagVariable.statusFlag = true
}
else if (currentStatus == EKAuthorizationStatus.denied) || (currentStatus == EKAuthorizationStatus.restricted){
    print("Access Has Been Denied")
    MainScreenTableView.flagVariable.statusFlag = false
}
}

我正在使用UITableViewController,这看起来是什么让这种痛苦。我现在拥有它的方式我正在设置一个标志,然后在tableview中我提出警报。但是,在用户拒绝访问后,我无法立即弹出警报。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

简单的解决方案是在eventInstance.requestAccess时调用accessGranted == false的完成处理程序内部显示提醒。

accessGrantedfalse一次性访问,并且用户点击拒绝或在用户运行应用程序之后的任何时间点击仍然拒绝访问。