#How to use ZbarSDK in Swift(XCode 6.2, base SDK iOS 8.2)
##Steps
- Download ZbarSDK and drag ZbarSDK folder in Swift project, setup buiding settings according to the zBarSDK tutorial
- Add #import "ZBarSDK.h" in YourApp_Bridging_Header file
- In your ViewController add ZBarReaderDelegate, setup ZBarReader, receive barcode data
##Posible building issues:
- Missing required architecture arm64 in file ...
Reason: the libzbar.a is not compiled to arm64 architecture
Solution: download libzbar.a compiled for arm64, and replace the libzbar.a in your project download link: (https://www.dropbox.com/sh/xoo1ql3yxzmu6wz/lV8vJFqJCE)
import UIKit
//This extension is used for the "for symbol in results as ZBarSymbolSet"
extension ZBarSymbolSet: SequenceType {
public func generate() -> NSFastGenerator {
return NSFastGenerator(self)
}
}
class MyViewController: UIViewController, UITextFieldDelegate, ZBarReaderDelegate {
var ZBarReader: ZBarReaderViewController?
@IBOutlet var barCodeText: UITextField!
@IBAction func scanAction(sender: AnyObject) {
if (self.ZBarReader == nil) {
self.ZBarReader = ZBarReaderViewController()
}
self.ZBarReader?.readerDelegate = self
self.ZBarReader?.scanner.setSymbology(ZBAR_UPCA, config: ZBAR_CFG_ENABLE, to: 1)
self.ZBarReader?.readerView.zoom = 1.0
self.ZBarReader?.modalInPopover = false
self.ZBarReader?.showsZBarControls = false
navigationController?.pushViewController(self.ZBarReader!, animated:true)
}
override func viewDidLoad() {
super.viewDidLoad()
self.barCodeText.delegate = self
}
func imagePickerController(reader:UIImagePickerController,
didFinishPickingMediaWithInfo info: NSDictionary) {
// ADD: get the decode results
var results: NSFastEnumeration = info.objectForKey(ZBarReaderControllerResults) as NSFastEnumeration
var symbolFound : ZBarSymbol?
for symbol in results as ZBarSymbolSet {
symbolFound = symbol as? ZBarSymbol
break
}
var resultString = NSString(string: symbolFound!.data)
self.barCodeText.text = resultString //set barCode
navigationController?.popViewControllerAnimated(true)
}
}
Use AVFoundation instead as ZBar for arm64 creates issues.
https://www.hackingwithswift.com/example-code/media/how-to-scan-a-barcode