Reading Barcodes on iOS 7

iOS 7 introduced APIs to read bar codes using the camera. I could have easily overlooked it without this excellent post from doubleencore.

NSHipster provided sample code, but it missed some details to work. Here is a sample which does.

Sample code


The issue I had with NSHipster’s sample code is the delegate method was not called at all. I quickly understood this was because the AVCaptureMetadataOutput must be configured to tell which metadata to recognise.

But, and this was not obvious to me, -[AVCaptureMetadataOuput setMetadataObjectTypes:] must be called after -[AVCaptureSession addOutput:]. Otherwise, the following message shows in the console:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVCaptureMetadataOutput setMetadataObjectTypes:] - unsupported type found.  Use -availableMetadataObjectTypes.'

I did try to look at the output of -availableMetadataObjectTypes, and it returns an empty array.

Therefore, -addOuput: must be called before setMetadataObjectTypes. In retrospect, it makes sense: the output object must know it is linked to a video session to know which metadata it may provide.

Add git tracking to an existing Xcode project

When creating a new project on Xcode 4, you are given the opportunity to activate git versioning  for this project.

You might have not checked this option in the first place and wish to add git tracking to the project later. Here is how to do it:

1) Open a Terminal window and go to the Project’s directory (the one that contains the .xcodeproj).

2) Type

This adds git version control to the directory and creates a .git repository. The project is now under version control.

3) Some files which should not be versioned. git uses a .gitignore text file for that purpose which contains a list of files (you may use wildcard characters).

To edit .gitignore, you may create an empty file:

Then open it in your favourite text editor.

I think it should at least contain:

You may also add:

Because you do not want information specific to a user (e.g. the list of breakpoints) to be pushed to a remote git repository.

4) To track project files (in other words, add them to the “staging area”), type

5) Finally, commit these files