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:
- Make sure that you add the
AVFoundation
Framework in the Link Binary section of build phases - You should write
import AVFoundation
on your class for importingAVFoundation
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
}
})
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
Using Ssl in an Iphone App - Export Compliance
Fade/Dissolve When Changing Uiimageview'S Image
Enterprise App Update Distribution on iOS 8
Table Header Views in Storyboards
Can Afnetworking Return Data Synchronously (Inside a Block)
Detecting Sheet Was Dismissed on iOS 13
How to Compare Two Uiimage Objects
How to Crop a Uiimageview to a New Uiimage in 'Aspect Fill' Mode
Swift - Stored Values Order Is Completely Changed in Dictionary
Resize Uiimage With Aspect Ratio
How to Determine the Os Version At Runtime in Os X or iOS (Without Using Gestalt)
Add an Element to an Array in Swift
How to Convert an Nsstring Value to Nsdata
How to Tell Swiftui Views to Bind to Nested Observableobjects