Camera Accessibility

by | February 4, 2019

Camera Accessibility

Jeff Summers

February 4, 2019

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 Privacy – Camera Usage Description. This needs to have a string value. From a development perspective, you can enter whatever you want into this value field. I entered $(PROGRAM_NAME) likes the camera and called it good.

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.

Share this Article

Posted by Jeff Summers

Author, technologist, and baseball aficionado specializing in information technology and developing new and creative ways to interact with the world around us. My goal is to extend the boundaries of what is possible and find ways to make the world a better place while having fun.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Stay Connected

Search

More Articles

Pace Layered Architecture Categorization

Change is inevitable. Today’s computing environments are constantly in flux from internal and external forces that put demands on the systems and necessitate changes to maintain its value stream. Technology currency is a constant struggle that organizations must...

TBM, Another Holy Grail?

Technology Business Management or TBM as a term was coined around 2012 although many of the concepts making up this framework have been around for almost as long as there has been IT. In its simplest form, TBM represents the integration of business, technology, and...

Visionary or Dreamer?

Thinking back over your life you’ve met and worked with countless personality types. You have been a part of teams that struggled to complete simple tasks and the work felt meaningless. Perhaps your goal in these times was to just trudge through the mud hoping the...

Archives