I am developing a very simple video app. I use the official control: UIImagePickerController.
Here is the problem. When presenting the UIImagePickerController for the first time, the iOS will ask for the permission. The user can click yes or no. If the user clicks no, the control is not dismissed. Instead, if the user keeps clicking the start button, the timers go on while the screen is always black, and the user can't stop the timers or go back. The only thing the user can do is to kill the app. The next time the UIImagePickerController is presented, it is still a black screen and the user can't go back if clicking start.
I was wondering if it's a bug. Is there any way we can detect the permission of the camera so that we can decide to show the UIImagePickerController or not?
Swift: Using AVFoundation
@IBAction func cameraButtonClicked(sender: AnyObject) {
Check the
AVAuthorizationStatus
and handle the cases properly.Swift 4 Solution
And then in order to use it you do
Since iOS 10 you need to specify
NSCameraUsageDescription
key in your Info.plist to be able ask for camera access, otherwise your app will crash at runtime. See APIs Requiring Usage Descriptions.Make sure to:
The Swift code below checks for all possible permission states:
Swift 4
Swift 3
As an interesting side note, did you know that iOS kills the app if it's running while you change its camera permissions in Settings?
From Apple Developer forum:
As an addition to the answer from @Raptor the following should be mentioned. You may receive the following error starting with iOS 10:
This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.
To fix this, make sure you handle the results from the main thread as follows (Swift 3):
Specify NSCameraUsageDescription key in Info.plist first. Then check AVAuthorizationStatus if Authorised then present the UIImagePickerController. It will work.