I went through a lot of pain, coming up with a solution that works on both the iPad and iPhone, this is the final code, which partially comes from other people's comments: the code has some errors, but this is a very good place to run :)
definitions:
__weak IBOutlet UIButton *attachButton; UIImage *image;
button action:
- (IBAction)doAttach:(id)sender { UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"Select image from" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"From library",@"From camera", nil] ; [action showInView:self.view]; } #pragma mark - ActionSheet delegates - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if( buttonIndex == 1 ) { AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; if(authStatus == AVAuthorizationStatusAuthorized) { NSLog(@"%@", @"You have camera access"); } else if(authStatus == AVAuthorizationStatusDenied) { NSLog(@"%@", @"Denied camera access"); [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { if(granted){ NSLog(@"Granted access to %@", AVMediaTypeVideo); } else { [self.presentedViewController dismissViewControllerAnimated:YES completion:nil]; UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"no camera access" message: @"if you need to use camera in this application go to settings -> appName -> and turn on camera." preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil]; NSLog(@"Not granted access to %@", AVMediaTypeVideo); return ; } }]; } else if(authStatus == AVAuthorizationStatusRestricted) { [self.presentedViewController dismissViewControllerAnimated:YES completion:nil]; UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"no camera access" message: @"if you need to use camera in this application go to settings -> appName -> and turn on camera." preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil]; NSLog(@"%@", @"Restricted, normally won't happen"); } else if(authStatus == AVAuthorizationStatusNotDetermined) { NSLog(@"%@", @"Camera access not determined. Ask for permission."); [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { if(granted){ NSLog(@"Granted access to %@", AVMediaTypeVideo); } else { NSLog(@"Not granted access to %@", AVMediaTypeVideo); return ; } }]; } else { [self.presentedViewController dismissViewControllerAnimated:YES completion:nil]; UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"No camera access" message: @"error accusing camera" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil]; return;
Omid S. Apr 09 '16 at 21:05 2016-04-09 21:05
source share