How to implement QR reader in swift

Posted By : Manu Gupta | 20-Mar-2015

QR code are basically the quick response code which are highy used for marketing these days and to store lengthy data so that user won't have to do this much of typing. A user can simply scan the QR Code and get all the information behind the code. Although it is also used for security purpose as the data behind the QR code cannot be seen by the naked eyes.

 

QR code reader in mobile application is must. So, i am going to share the way to implement a model in Swift i.e "Simple QRCode reader in Swift" which is based on AVFoundation framework from Apple.

 

  1. Firstly, download the project from Git Hub and copy its QRCodeReader folder and place it into your project.
  2. Now for usage do to your ViewController and add QRCodeReaderDelegate.
                                class WalletViewController: UIViewController,UITextFieldDelegate,QRCodeReaderDelegate{
                                //initialize the qrReader 
                                lazy var qrReader: QRCodeReader = QRCodeReader(cancelButtonTitle: "Cancel Scan")
                            
  3. After this add the following code on your button click:
                                //Set the delegate property
                                qrReader.delegate = self
                                
                                //On completion of scan print the result
                                qrReader.completionBlock = {(result: String?) in print(result)}
                                qrReader.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
                                presentViewController(qrReader, animated: true, completion: nil)
                            
  4. Implement the QRReader delegate methods in your code:
                                //This is the method which will return the result to your previous controller
                                func reader(reader: QRCodeReader, didScanResult result: String) {
                                    println(result)
                                    self.dismissViewControllerAnimated(true, completion: nil)
                                }
                                //This method will be fired when you click on the cancel button
                                func readerDidCancel(reader: QRCodeReader) {
                                    self.dismissViewControllerAnimated(true, completion: nil)
                                }
                            
  5. Test this on your device.

      Thanks and for any query feel free to contact @[email protected]

About Author

Author Image
Manu Gupta

Manu is a Native iOS (Swift) and Titanium lead developer . Manu has good experience working with Swift and Alloy framework as well. He loves watching movies and travelling.

Request for Proposal

Name is required

Comment is required

Sending message..