Implementing a QR Code reader on the iPhone
QR Codes are becoming increasingly popular but few people realize just how easy it is to support this technology from an iPhone application.
In this example, I’ll illustrate how to knock together a simple QR Code reader using the open source ZBarSDK. It’s not going to be an ‘All singing, all dancing’ application, but it will be sufficient to illustrate the point.
The following example assumes that you’re already familiar with both XCode and iPhone development.
Step 1: Create a Window-based iPhone application and name it ‘QRReader’
Step 2: Download ZBarSDK
Step 3: Run the ZBarSDK installer and follow the simple instructions for adding the SDK to your XCode project
Step 4: Add the following frameworks to your project:
Step 5: Add the main view to the Application Delegate, as follows:
At this point ZBarSDK is already integrated with the video feed from the camera, but we still need to implement the delegate so that the SDK can notify us when it has managed to detect a QR Code.
The following delegate method should now be added to the Application Delegate:
Step 6: Next we need to create a new UIViewController subclass and call it ‘QRCodeDetail’. This will be used to show the details of the QR Code once it has been read by the main view.
Update the header file to contain the following:
Remember to synthesize the variables:
Finally we update the ‘viewDidLoad’ method:
Step 7: Hook the QRCodeDetail view into the main view
Back in the Application Delegate we need to import our newly created view:
Then we need to update the ‘didFinishPickingMediaWithInfo’ method to load the ‘QRCodeDetail’ view whenever a QR Code is detected:
Step 8: Simply install on an iPhone and enjoy the QR Code Reader.
You can easily generate your own QR Codes here.