This is part four of the 100 Days of Swift challenge. You can find Part one of this series here. During one of my attempts at creating an iPhone app, I wrote a program that needed to allow the user to capture an image from the phone’s camera. Following along with the documentation that Apple provides along with a healthy plate full of Google searches provided me with what I thought was necessary to access and use the camera from within the application.
I used the Storyboard within XCode to layout what the screen would look like and added a button for the camera. The user would click the camera icon which would access the device’s camera and allow the user to take a picture.
After including an action within the View Controller to connect the button to the code file, I added the following lines of code:
var imagePicker = UIImagePickerController()
@IBAction func CameraTapped(_ sender: Any) {
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
This is not all the code but just a portion dealing with the action that the camera button was tapped. Apple does a pretty good job of managing a lot of things for the programmer so this could have been a lot uglier than it is.
I had similar code for accessing the photos on the device and had tested that on the Xcode Simulator and it worked as planned. It should be noted, you cannot test the camera on the Xcode Simulator, it will throw an error and crash. So to test the camera you need to install the application on a phone and test on an actual device.
I was brimming with confidence as I ran the code only to find the application completely died whenever I hit the camera icon. I went through the standard troubleshooting guides assuming I had made some kind of coding mistake. But the code looked valid so why wouldn’t it run?
A few searches on Google and some questions in the various forums I found led me to a little piece of information that from the looks of the number of people asking the question, the answer is not widely known.
When an iPhone app wants to use the Camera, Apple looks at this as a privacy issue. The device will therefore ask the user’s permission to grant an application the ability to use the camera. A modal dialog box is presented that asks permission. There is a piece of data that the application must provide for this to happen. There needs to be an entry within the application’s info.plist.
The key necessary for this is called
Rebuilding the application and installing it again on the iPhone I ran the app and selected the camera icon. The dialog box opened asking for permission to use the camera including the rather cheesy description I included. Once allowing access, the app worked as expected.
I thought that was the end to this saga. I just needed to make sure there was something in the Privacy – Camera Usage Description value field. Subsequently I learned that Apple expects that value to be properly filled out with a good reason. During the application review process that a developer must go through to have their app included in the App Store, they will test the app and if you put cheesy or bogus information in that field your app will get rejected. So if your application needs access to the camera or the photo library, take the time to explain in the info.plist file why the app needs this access otherwise you may not pass the Apple App Review process needed for app distribution.
0 Comments