A QR Code Scanning and Generating library for iOS 8+, written in Swift 4.
iOS 8+
QRBot is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'QRBot'
Follow the steps to use QRBot:
- Steps for scanning QR Code:
- You need to give permission to access Camera in
Info.plist
.
- Make
QRCodeScannerViewController
superclass of your ViewController class.
class ViewController: QRCodeScannerViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
- Conform/ Implement
QRCodeScannerDelegate
protocol and implement its method to perform your tasks.
class ViewController: QRCodeScannerViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension ViewController: QRCodeScannerDelegate{
func scanner(_ connection: AVCaptureConnection, didOutput data: String) {
print("DemoApp : Data from QR Code - \(data)")
// Perform your actions here with data from QRCode
}
}
( If AVCaptureConnection
may show any error, just import AVFoundation
in your ViewController. )
import UIKit
import AVFoundation
import QRBot
class ViewController: QRCodeScannerViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension ViewController: QRCodeScannerDelegate{
func scanner(_ connection: AVCaptureConnection, didOutput data: String) {
print("DemoApp : Data from QR Code - \(data)")
// Perform your actions here with data from QRCode
}
}
- Set
scannerDelegate
property of your ViewController toself
.
class ViewController: QRCodeScannerViewController {
override func viewDidLoad() {
super.viewDidLoad()
scannerDelegate = self
}
}
- All set. Now implement your tasks in delegate method.
- Steps for generating QR Code from your own data:
- Import QRBot in your ViewController.
import QRBot
- Call
generate()
method ofQRCode
class as shown below:
import UIKit
import QRBot
class YourViewController: UIViewController{
ovveride func viewDidLoad(){
super.viewDidLoad()
QRCode.generate(from: yourStringData,completion: { (image,error) in
if error == nil{
if let qrCodeImage = image{
// Set qrCodeImage to your ImageView
self.imageView.image = qrCodeImage // For example
}
}
})
}
}
-
Below the list of supported methods are given. Their usages are as same as shown above.
- Simple Black & White QR Code from your String data
QRCode.generate(from data: String, completion: @escaping (UIImage?,Error?) -> Void)
- Simple Black & White QRCode from your String data, with Image format you wish
QRCode.generate(from data: String, imageFormat format: QRCodeImageFormat,completion: @escaping (UIImage?,Error?) -> Void)
Here,
QRCodeImageFormat
is enum having values like.png
,.jpg
,.jpeg
,.gif
.- QRCode with different Pattern Colour and Background Colour
QRCode.generate(from data: String, patternColor color: QRCodeColor,backgroundColor bgcolor: QRCodeColor, completion: @escaping (UIImage?,Error?) -> Void)
Here,
QRCodeColor
is enum, having all colours from Apple Colour Pallete.- QRCode with different Pattern Colour and Background Colour, with Image format you wish
QRCode.generate(from data: String, patternColor color: QRCodeColor,backgroundColor bgcolor: QRCodeColor, imageFormat format: QRCodeImageFormat, completion: @escaping (UIImage?,Error?) -> Void)
Take a look at Example Project for more clarity. To run the example project, clone the repo, and run pod install
from the Example directory first.
Hardik Trivedi, hardiktrivedi3008.ht@gmail.com
Made with ❤️ in India.