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.
- Firstly, download the project from Git Hub and copy its QRCodeReader folder and place it into your project.
- 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")
- 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)
- 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) }
- Test this on your device.
Thanks and for any query feel free to contact @[email protected]
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.