Skip to content

Instantly share code, notes, and snippets.

@carstenhag
Created July 19, 2018 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carstenhag/f21300611bc1ffecfa386e698b7429f2 to your computer and use it in GitHub Desktop.
Save carstenhag/f21300611bc1ffecfa386e698b7429f2 to your computer and use it in GitHub Desktop.
// Created by Carsten Hagemann @ Kaufland Informationssysteme
// With code from the SDK docs: https://help.sap.com/doc/978e4f6c968c4cc5a30f9d324aa4b1d7/Latest/en-US/Documents/Frameworks/SAPFiori/Classes/FUIBarcodeScanViewController.html
import UIKit
import SAPFiori
class ScanViewController: UIViewController, FUIBarcodeScanViewControllerDelegate {
private var scanViewController: FUIBarcodeScanViewController?
@IBOutlet weak var startScanView: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
#if targetEnvironment(simulator)
print("\nScanning is not possible in the Simulator!\n")
#endif
scanViewController = FUIBarcodeScanViewController.createInstanceFromStoryboard()
guard let safeScanViewController = scanViewController, safeScanViewController.barcodeScanner != nil else {
return
}
safeScanViewController.barcodeScanner.scanMode = .qr
safeScanViewController.barcodeScanner.indicatorBorderColor = UIColor.green.cgColor
safeScanViewController.barcodeScanner.indicatorBorderWidth = 5
safeScanViewController.barcodeScanner.promptMessage = "Scan A QR Code"
safeScanViewController.barcodeScanner.scanResultTransformer = { scanString in
// Modify the scanned String here
return scanString
}
safeScanViewController.delegate = self
self.startScanView.addTarget(nil, action: #selector(showScanView), for: .touchUpInside)
}
@objc func showScanView() {
guard let safeScanViewController = scanViewController else {
return
}
let navController = UINavigationController(rootViewController: safeScanViewController)
self.navigationController?.present(navController, animated: true, completion: nil)
}
func barcodeScanViewController(_ barcodeScanViewController: FUIBarcodeScanViewController, didReceiveScanResult scanResult: FUIBarcodeScanResult?) {
print("Scanned '\(scanResult?.scanResultString ?? "")'")
let scanSuccessful = (scanResult?.scanResultString.count ?? 0) > 3 // Move this out as a method
if scanSuccessful {
barcodeScanViewController.dismiss(animated: true, completion: nil)
} else {
print(scanResult?.scanResultString as Any)
guard let safeScanViewController = scanViewController else {
return
}
safeScanViewController.barcodeScanner.promptMessage = "QR Code was not valid. Please scan again."
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment