The SocketScan API for iOS is written in Objective-C and is intended to integrate SocketMobile scanners in CHS mode into native applications.
When the ScanApi is initiated, requires the ViewController to be set as a ScanApiHelperDelegate, which requires some setup on the viewDidLoad as well as a few functions for receiving actions from the scanner.
Vars are setup:
var scanApi = ScanApiHelper()
var scanApiConsumer = NSTimer()
Then in the viewDidLoad, the following code is implemented:
scanApi.setDelegate(self)
scanApi.open()
scanApiConsumer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector:Selector("onTimer"), userInfo: nil, repeats: true)
Then, the following function is called by scanApiConsumer to listen for notifications from the scanner:
func onTimer () -> Void{
scanApi.doScanApiReceive()
scanApi.getDevicesList()
}
When a barcode is scanned, it performs the following:
func onDecodedData(device: DeviceInfo, decodedData: ISktScanDecodedData) {
//code to execute here
}
If needed, the user is routed to another view where scanning a barcode executes different code. The scanApi.setDelegate is set like this:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "manifestToRepackContainer" {
let vc = (segue.destinationViewController as RepackContainer)
scanApi.setDelegate(vc)
vc.containerBarcode = GlobalVars.barcodeData
}
}
That works great too. Once you are there, the scanner performs the designated functions properly. And, on viewDidAppear on the original viewer, I have this setup, which also works as intended:
override func viewDidAppear(animated: Bool) {
scanApi.setDelegate(self)
}
The issue arises when the user needs scanning functionality in a completely different area of the storyboard (the 'remote' view), where a prepareForSegue method isn't possible to trigger a scanApi.setDelegate()
My initial thought was to simply define scanApi delegate in the "remote" view by executing a scanApi.setDelegate(self) in viewDidAppear on the "remote" view. Of course, this view is also designated as a scanApiHelperDelegate and contains all the needed functions. However, it doesn't work. The original viewController is still the delegate and scanning barcode continue to trigger the functions listed in the original viewController, not the current one. The compiler returns no errors.
I suspect it is either a) something about the way Swift interacts with the Objective-C API or b) i am incorrectly declaring what should be the new scanApi delegate by using "self".
So, what is the proper way to declare the new scanApi delegate in this scenario or is this an issue with the API?
The current version of ScanApiHelper doesn't support multiple delegate views, but future versions will. Here are the modifications we made, if you'd like to add it to the ScanApiHelper in your project.
Note: The changes are backwards compatible, so you can safely apply these changes or upgrade to the latest version of ScanApiHelper when it is released even if you don't need to support multiple delegate views
ScanApiHelper.h
ScanApiHelper.mm