Search code examples
objective-cswiftbarcode-scannersocketscansocketmobile

Socket Mobile SocketScan SDK and Swift


I am attempting to use the SocketScan SDK with an application written in Swift. Using a bridging header, the Objective-C code was easily imported. However,when running the application, it crashes at the onDeviceArrival stage with Xcode citing an "unrecognized selector". That is, the ScanApi open and runs and on the arrival of a device, the application crashes with the following error:

-[SocketConnect.ViewController onDeviceArrival:Device:]: unrecognized selector sent to instance 0x15550e670 * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SocketConnect.ViewController onDeviceArrival:Device:]: unrecognized selector sent to instance 0x15550e670' * First throw call stack: (0x185a9259c 0x1961e40e4 0x185a99664 0x185a96230 0x18599ab6c 0x1000b2798 0x1000b2148 0x1000b1e24 0x1000a2dc4 0x1000a2e0c 0x186966e18 0x185a4a8d8 0x185a4a588 0x185a47fd4 0x1859750a4 0x18eb175a4 0x18a2aa3c0 0x1000a6ae8 0x1000a6b28 0x196852a08) libc++abi.dylib: terminating with uncaught exception of type NSException

Here is the code snippet from onDeviceArrival as supplied by the API.

-(SKTRESULT)handleDeviceArrival:(id<ISktScanObject>)scanObj{
SKTRESULT result=ESKT_NOERROR;
id<ISktScanDevice> scanDevice=[SktClassFactory createDeviceInstance:_scanApi];
NSString* name=[[scanObj Msg]DeviceName];
NSString* guid=[[scanObj Msg]DeviceGuid];
long type=[[scanObj Msg]DeviceType];

// create a new DeviceInfo object
DeviceInfo* deviceInfo=[[DeviceInfo alloc]init:scanDevice name:name type:type];

// open the scanner which means that we can now receive
// any event (such as DecodedData event) from this scanner
result=[scanDevice open:guid];

NSLog(@"!!!device connected!!!!");

if(SKTSUCCESS(result)){
NSLog(@"SKTSUCCESS!");
    if(_noDeviceText!=nil)
        [_deviceInfoList removeObjectForKey:_noDeviceText];

    // add the device info into the list
    [_deviceInfoList setValue:deviceInfo forKey:[NSString stringWithFormat:@"%@",scanDevice]];
    NSLog(@"device info added to list.");
}

// notify the ScanApiHelper user a scanner has connected to this host
NSLog(@"about to notify ScanApiHelper that the scanner has been connected.");

if(_delegate!=nil)
    NSLog(@"delegate is NOT nil");

   [_delegate onDeviceArrival:result Device:deviceInfo];
 NSLog(@"ScanApi has been notified");
enter code here`#if __has_feature(objc_arc)
#else 
[deviceInfo release];// we don't keep this object since we couldn't open the scanner
 NSLog(@"device info released");
#endif
return result;
}

The log prints everything up to and including "delegate is NOT nil".

Has anyone had issues with the SocketScan SDK running in a Swift environment and, if so, have any recommendations to overcoming this issue?


Solution

  • I had the same problem. The selector "Device" is in lowercase in Swift for the delegate func onDeviceArrival.

    Change the "Device" to "device" in ScanAPI/lib/ScanApiHelper.h (ScanApiHelperDelegate) as

    -(void)onDeviceArrival:(SKTRESULT)result device:(DeviceInfo*)deviceInfo;
    

    Then change the call in ScanAPI/lib/ScanApiHelper.mm in function handleDeviceArrival

    [_delegate onDeviceArrival:result device:deviceInfo];
    

    Hope this helps!