с Swift 5 это просто и супер быстро !!
Вам просто нужно добавить какао-бобы "BarcodeScanner", вот полный код
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '12.0'
target 'Simple BarcodeScanner'
do
pod 'BarcodeScanner'
end
Убедитесь, что добавили разрешение Camera в свой файл .plist
<key>NSCameraUsageDescription</key>
<string>Camera usage description</string>
И добавьте Сканер и обработайте результат в вашем ViewController следующим образом
import UIKit
import BarcodeScanner
class ViewController: UIViewController, BarcodeScannerCodeDelegate, BarcodeScannerErrorDelegate, BarcodeScannerDismissalDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let viewController = BarcodeScannerViewController()
viewController.codeDelegate = self
viewController.errorDelegate = self
viewController.dismissalDelegate = self
present(viewController, animated: true, completion: nil)
}
func scanner(_ controller: BarcodeScannerViewController, didCaptureCode code: String, type: String) {
print("Product's Bar code is :", code)
controller.dismiss(animated: true, completion: nil)
}
func scanner(_ controller: BarcodeScannerViewController, didReceiveError error: Error) {
print(error)
}
func scannerDidDismiss(_ controller: BarcodeScannerViewController) {
controller.dismiss(animated: true, completion: nil)
}
}
Еще и любые вопросы или проблемы, , пожалуйста, проверьте пример приложения здесь с полным исходным кодом