* Photos
func requestForAccessPhotos(view : UIViewController, completionHandler: (accessGranted: Bool) -> Void){
let authorizationStatus = PHPhotoLibrary.authorizationStatus()
switch authorizationStatus {
case .Authorized:
completionHandler(accessGranted: true)
case .Denied, .NotDetermined:
PHPhotoLibrary.requestAuthorization() {(status) -> Void in
switch status {
case .Authorized:
completionHandler(accessGranted: true)
case .Denied, .Restricted:
dispatch_async(dispatch_get_main_queue(), { () -> Void in
Handler().permissionAlert(view, title: "Access Denied", message: "Please allow the app to access your photos through the Settings.")
})
default:
completionHandler(accessGranted: false)
}
}
default:
completionHandler(accessGranted: false)
}
}
* Contacts
func requestForAccessContacts(view : UIViewController, completionHandler: (accessGranted: Bool) -> Void) {
let authorizationStatus = CNContactStore.authorizationStatusForEntityType(CNEntityType.Contacts)
switch authorizationStatus {
case .Authorized:
completionHandler(accessGranted: true)
case .Denied, .NotDetermined:
contactStore.requestAccessForEntityType(CNEntityType.Contacts, completionHandler: { (access, accessError) -> Void in
if access {
completionHandler(accessGranted: access)
}
else {
if authorizationStatus == CNAuthorizationStatus.Denied {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
Handler().permissionAlert(view, title: "Access Denied", message: "Please allow the app to access your contacts through the Settings.")
})
}
}
})
default:
completionHandler(accessGranted: false)
}
}
* Photos
func requestForAccessCalendar(view : UIViewController, send : UISwitch, completionHandler : (accessGranted : Bool) -> Void){
let authorizationStatus = EKEventStore.authorizationStatusForEntityType(EKEntityType.Event)
switch authorizationStatus {
case .Authorized:
completionHandler(accessGranted: true)
case .Denied, .NotDetermined:
let eventStore = EKEventStore()
eventStore.requestAccessToEntityType(EKEntityType.Event, completion: { (access, accessError) -> Void in
if access {
completionHandler(accessGranted: access)
}
else {
if authorizationStatus == EKAuthorizationStatus.Denied {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
Handler().permissionAlert(view, title: "Access Denied", message: "Please allow the app to access your calendar through the Settings.")
send.setOn(false, animated:true)
})
}
}
})
default:
completionHandler(accessGranted: false)
}
}
* Camera
func requestForAVCature(view : UIViewController, completionHandler : (accessGranted : Bool) -> Void){
let authorizationStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)
switch authorizationStatus {
case .Authorized:
completionHandler(accessGranted: true)
case .Denied, .NotDetermined:
AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (granted : Bool) -> Void in
if(granted){ // Access has been granted ..do something
completionHandler(accessGranted: true)
} else { // Access denied ..do something
if authorizationStatus == AVAuthorizationStatus.Denied {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
Handler().permissionAlert(view, title: "Access Denied", message: "Please allow the app to access your calendar through the Settings.")
})
}
}
})
default:
completionHandler(accessGranted: false)
}
}
* Inside the 'Handler' class
func permissionAlert(view: UIViewController, title : String?, message : String?){
let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
view.presentViewController(alertController, animated: true, completion: nil)
}