Detect Permission of Camera in iOS

Detect permission of camera in iOS

Check the AVAuthorizationStatus and handle the cases properly.

NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
if(authStatus == AVAuthorizationStatusAuthorized) {
// do your logic
} else if(authStatus == AVAuthorizationStatusDenied){
// denied
} else if(authStatus == AVAuthorizationStatusRestricted){
// restricted, normally won't happen
} else if(authStatus == AVAuthorizationStatusNotDetermined){
// not determined?!
[AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
if(granted){
NSLog(@"Granted access to %@", mediaType);
} else {
NSLog(@"Not granted access to %@", mediaType);
}
}];
} else {
// impossible, unknown authorization status
}

How to check if the user gave permission to use the camera?

You can use the following code for doing the same:

if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) ==  AVAuthorizationStatus.Authorized {
// Already Authorized
} else {
AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (granted: Bool) -> Void in
if granted == true {
// User granted
} else {
// User rejected
}
})
}

NOTE:

  1. Make sure that you add the AVFoundation Framework in the Link Binary section of build phases
  2. You should write import AVFoundation on your class for importing AVFoundation

SWIFT 3

if AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo) ==  AVAuthorizationStatus.authorized {
// Already Authorized
} else {
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (granted: Bool) -> Void in
if granted == true {
// User granted
} else {
// User Rejected
}
})
}

Swift 4

if AVCaptureDevice.authorizationStatus(for: .video) ==  .authorized {
//already authorized
} else {
AVCaptureDevice.requestAccess(for: .video, completionHandler: { (granted: Bool) in
if granted {
//access allowed
} else {
//access denied
}
})
}

How to detect privacy permission changes (Camera access for example)

So, this turned out to be a bug related to iOS 9b1.

Permission detection works fine on iOS 8.

I did learn that you need to check the permission on the main queue. If you do that, it will reflect updates.

How can I get the authorization status to Photo/Camera on IOS?

the question detect permission of camera on ios mentioned the class AVCaptureDevice, it can fix the authorization of Camera Usage.

I found the solution to the authorization of 'photo'. There is a new framework called Photos in IOS8 and a class PHPhotoLibrary.

This method can give an Alert:

PHPhotoLibrary.requestAuthorization({(status: PHAuthorizationStatus)in
switch status{
case .denied:
break
case .authorized:
break
default:
break
}
})

Sample Image

Just like when UIImagePickerController appears the first time.

and class func authorizationStatus() -> PHAuthorizationStatus can return the current authorization status of 'photo album'

How to check in Swift if the user gave permission to access the photolibrary with UIImagePicker?

You can use the below method to check the permission status for the gallery.
and if the permission is denied then also it navigates you to the app setting screen.

func checkGalleryPermission()
{
let authStatus = PHPhotoLibrary.authorizationStatus()
switch authStatus
{
case .denied : print("denied status")
let alert = UIAlertController(title: "Error", message: "Photo library status is denied", preferredStyle: .alert)
let cancelaction = UIAlertAction(title: "Cancel", style: .default)
let settingaction = UIAlertAction(title: "Setting", style: UIAlertAction.Style.default) { UIAlertAction in
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url, options: [:], completionHandler: { _ in })
}
}
alert.addAction(cancelaction)
alert.addAction(settingaction)
Viewcontoller.present(alert, animated: true, completion: nil)
break
case .authorized : print("success")
//open gallery
break
case .restricted : print("user dont allowed")
break
case .notDetermined : PHPhotoLibrary.requestAuthorization({ (newStatus) in
if (newStatus == PHAuthorizationStatus.authorized) {
print("permission granted")
//open gallery
}
else {
print("permission not granted")
}
})
break
case .limited:
print("limited")
@unknown default:
break
}
}


Related Topics



Leave a reply



Submit