I want to request some permissions and want to return false unless all permissions have been granted. I get an error: "Unexpected non-void return value in void function"
But I am returning true/false in all circumstances, what is wrong ?
func requestPermissions () -> Bool {
let types: UIRemoteNotificationType = [.alert, .badge, .sound]
UIApplication.shared.registerForRemoteNotifications(matching: types)
let center = UNUserNotificationCenter.current()
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (videoGranted: Bool) -> Void in
if (videoGranted) {
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeAudio, completionHandler: { (audioGranted: Bool) -> Void in
if (audioGranted) {
center.requestAuthorization(options: [.alert, .badge, .sound]) { (notificationGranted:Bool, error) -> Void in
if (notificationGranted) {
DispatchQueue.main.async {
return true
print("Video, audio & notifications granted")
}
} else {
return false
print("Rejected notifications")
}
}
}else {
return false
print("Rejected audio")
}
})
} else {
return false
print("Rejected video")
}
})
}
Any help would be very much appreciated ! Thank you.
Alternative Answer:
func requestPermissionss (completion: ((Bool, Bool, Bool)->Void)?) {
let types: UIRemoteNotificationType = [.alert, .badge, .sound]
UIApplication.shared.registerForRemoteNotifications(matching: types)
let center = UNUserNotificationCenter.current()
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (videoGranted: Bool) -> Void in
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeAudio, completionHandler: { (audioGranted: Bool) -> Void in
center.requestAuthorization(options: [.alert, .badge, .sound]) { (notificationGranted:Bool, error) -> Void in
completion?(videoGranted, audioGranted, notificationGranted)
}
})
})
}