Skip to content

Instantly share code, notes, and snippets.

@iandundas
Created March 16, 2015 16:19
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save iandundas/346d119e665582ec85e1 to your computer and use it in GitHub Desktop.
Save iandundas/346d119e665582ec85e1 to your computer and use it in GitHub Desktop.
How to detect iOS Photo library or Camera permissions (>iOS8)
//
// Created by Ian Dundas on 16/03/15.
//
#import "UIImagePickerController+cats.h"
#import <AVFoundation/AVFoundation.h>
#import "Photos/Photos.h"
/*
example usage:
[UIImagePickerController obtainPermissionForMediaSourceType:UIImagePickerControllerSourceTypePhotoLibrary withSuccessHandler:^{
UIImagePickerController *pickerNavController = [[UIImagePickerController alloc] init];
pickerNavController.delegate = self;
pickerNavController.allowsEditing = NO;
pickerNavController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:pickerNavController animated:YES completion:nil];
} andFailure:^{
UIAlertController *alertController= [UIAlertController
alertControllerWithTitle:nil
message:NSLocalizedString(@"You have disabled Photos access", nil)
preferredStyle:UIAlertControllerStyleActionSheet];
[alertController addAction:[UIAlertAction
actionWithTitle:NSLocalizedString(@"Open Settings", @"Photos access denied: open the settings app to change privacy settings")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}]
];
[alertController addAction:[UIAlertAction
actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel")
style:UIAlertActionStyleDefault
handler:NULL]
];
[self presentViewController:alertController animated:YES completion:^{}];
}];
*/
@implementation UIImagePickerController (Additions)
+ (void)obtainPermissionForMediaSourceType:(UIImagePickerControllerSourceType)sourceType withSuccessHandler:(void (^) ())successHandler andFailure:(void (^) ())failureHandler {
if (sourceType == UIImagePickerControllerSourceTypePhotoLibrary || sourceType == UIImagePickerControllerSourceTypeSavedPhotosAlbum){
// Denied when photo disabled, authorized when photos is enabled. Not affected by camera
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
switch (status) {
case PHAuthorizationStatusAuthorized: {
if (successHandler)
dispatch_async (dispatch_get_main_queue (), ^{ successHandler (); });
}; break;
case PHAuthorizationStatusRestricted:
case PHAuthorizationStatusDenied:{
if (failureHandler)
dispatch_async (dispatch_get_main_queue (), ^{ failureHandler (); });
}; break;
default:
break;
}
}];
}
else if (sourceType == UIImagePickerControllerSourceTypeCamera){
// Checks for Camera access:
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (status){
case AVAuthorizationStatusAuthorized:{
if (successHandler)
dispatch_async (dispatch_get_main_queue (), ^{ successHandler (); });
}; break;
case AVAuthorizationStatusNotDetermined:{
// seek access first:
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if(granted){
if (successHandler)
dispatch_async (dispatch_get_main_queue (), ^{ successHandler (); });
} else {
if (failureHandler)
dispatch_async (dispatch_get_main_queue (), ^{ failureHandler (); });
}
}];
}; break;
case AVAuthorizationStatusDenied:
case AVAuthorizationStatusRestricted:
default:{
if (failureHandler)
dispatch_async (dispatch_get_main_queue (), ^{ failureHandler (); });
}; break;
}
}
else{
NSAssert(NO, @"Permission type not found");
}
}
@end
//
// Created by Ian Dundas on 16/03/15
//
#import <Foundation/Foundation.h>
@interface UIImagePickerController (cats)
+ (void)obtainPermissionForMediaSourceType:(UIImagePickerControllerSourceType)sourceType withSuccessHandler:(void (^) ())successHandler andFailure:(void (^) ())failureHandler;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment