My iOS app asks permission to use the camera (to scan a barcode). When they allow it, the app runs fine.
When they deny it, my app crashes when the view that uses the camera is opened.
How can I prevent this and show a message instead?
Do you have a crash log you can share?
Clearly when the user denies access, you shouldn’t even open the view.
Most of these permissions based technologies expect you to request permission on every launch and store that state in your app when the response comes back.
You could also use a declare to find out what that state is
Fair point @Greg_O. But how do I get the state?
Public Function CanUseCamera() As integer
// + (AVAuthorizationStatus) authorizationStatusForMediaType:(AVMediaType) mediaType;
Declare Function authorizationStatusForMediaType Lib "Foundation" Selector "authorizationStatusForMediaType:" ( cls As Ptr , type As CFStringRef ) As Integer
Declare Function NSClassFromString Lib "Foundation" (name As CFStringRef) As Ptr
Dim n As Integer = authorizationStatusForMediaType(NSClassFromString("AVFoundation"), "AVMediaTypeVideo")
Select Case n
Case 0
// not determined
Case 1
// restricted (parental controls, etc)
Case 2
// denied
Case 3
// granted
Case 4
// limited
End Select
return n
End Function
for best results, put that return value in an enum.
You may need to change that string to “vide”
Thanks Greg, the status is undetermined until the user access the screen that has the camera view. Is there a way I can trigger the request for access before by code? Now it’s automatic, but only when the user gets to the camera view.
If the user denies access there, and relaunches, the status is for some reason still undetermined.
I ended up using the MBS iOS plugin for AVfoundation. after adding a AVfoundation property the next line in the open event returns the actual state of the camera;
statusCamera = AVCaptureDeviceMBS.authorizationStatusForMediaType(AVFoundationMBS.AVMediaTypeVideo)
When that status is 2 (denied) I display a message to enable the camera and relaunch the app, and disable the button to start using the camera