- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
适用于 iOS 的 SocketScan API 是用 Objective-C 编写的,旨在将 CHS 模式下的 SocketMobile 扫描器集成到 native 应用程序中。
启动 ScanApi 时,需要将 ViewController 设置为 ScanApiHelperDelegate,这需要在 viewDidLoad 上进行一些设置以及一些用于从扫描器接收操作的函数。
设置变量:
var scanApi = ScanApiHelper()
var scanApiConsumer = NSTimer()
然后在viewDidLoad中,实现了如下代码:
scanApi.setDelegate(self)
scanApi.open()
scanApiConsumer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector:Selector("onTimer"), userInfo: nil, repeats: true)
然后,scanApiConsumer 调用以下函数来监听来自扫描器的通知:
func onTimer () -> Void{
scanApi.doScanApiReceive()
scanApi.getDevicesList()
}
扫描条形码时,它会执行以下操作:
func onDecodedData(device: DeviceInfo, decodedData: ISktScanDecodedData) {
//code to execute here
}
如果需要,用户会被引导到另一个 View ,在该 View 中扫描条形码会执行不同的代码。 scanApi.setDelegate 设置如下:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "manifestToRepackContainer" {
let vc = (segue.destinationViewController as RepackContainer)
scanApi.setDelegate(vc)
vc.containerBarcode = GlobalVars.barcodeData
}
}
这也很好用。一旦您到达那里,扫描仪就会正确执行指定的功能。而且,在原始查看器的 viewDidAppear 上,我有这个设置,它也可以按预期工作:
override func viewDidAppear(animated: Bool) {
scanApi.setDelegate(self)
}
当用户需要在 Storyboard的一个完全不同的区域(“远程” View )中扫描功能时,就会出现问题,其中 prepareForSegue 方法不可能触发 scanApi.setDelegate()
我最初的想法是通过在“远程” View 的 viewDidAppear 中执行 scanApi.setDelegate(self) 来简单地在“远程” View 中定义 scanApi 委托(delegate)。当然,此 View 也被指定为 scanApiHelperDelegate 并包含所有需要的功能。但是,它不起作用。原来的 viewController 仍然是 delegate,扫描条码继续触发原 viewController 中列出的功能,而不是当前的。编译器不会返回任何错误。
我怀疑是 a) Swift 与 Objective-C API 交互的方式或 b) 我使用“self”错误地声明了新的 scanApi 委托(delegate)应该是什么。
那么,在这种情况下声明新的 scanApi 委托(delegate)的正确方法是什么,或者这是 API 的问题?
最佳答案
当前版本的 ScanApiHelper 不支持多个委托(delegate) View ,但 future 的版本会支持。如果您想将它添加到项目中的 ScanApiHelper,这是我们所做的修改。
注意:更改是向后兼容的,因此您可以安全地应用这些更改或在发布时升级到最新版本的 ScanApiHelper,即使您不需要支持多个委托(delegate) View 也是如此
ScanApiHelper.h
Index: ScanApiHelper.h
===================================================================
--- ScanApiHelper.h (revision 12778)
+++ ScanApiHelper.h (revision 12779)
@@ -65,6 +65,7 @@
@end
@protocol ScanApiHelperDelegate <NSObject>
+@optional
/**
* called each time a device connects to the host
* @param result contains the result of the connection
@@ -103,7 +104,6 @@
*/
-(void) onErrorRetrievingScanObject:(SKTRESULT) result;
-@optional
/**
* called each time ScanAPI receives decoded data from scanner
* @param result is ESKT_NOERROR when decodedData contains actual
@@ -179,9 +179,15 @@
id<ISktScanApi>_scanApi;
id<ISktScanObject>_scanObjectReceived;
NSObject* _commandContextsLock;
+ BOOL _shared;// to indicate when ScanApiHelper is shared across multiple views
+ NSMutableArray* _delegateStack;
}
++(ScanApiHelper*)sharedScanApiHelper;
+-(void)pushDelegate:(id<ScanApiHelperDelegate>)delegate;
+-(void)popDelegate:(id<ScanApiHelperDelegate>)delegate;
+
/**
* register for notifications in order to receive notifications such as
* "Device Arrival", "Device Removal", "Decoded Data"...etc...
ScanApiHelper.mm
Index: ScanApiHelper.mm
===================================================================
--- ScanApiHelper.mm (revision 12778)
+++ ScanApiHelper.mm (revision 12779)
@@ -105,12 +105,16 @@
@implementation ScanApiHelper
-(id)init{
+ static ScanApiHelper* sharedScanApiHelper=nil;
self=[super init];
if(self!=nil){
+ sharedScanApiHelper=self;
_commandContextsLock=[[NSObject alloc]init];
_deviceInfoList=[[NSMutableDictionary alloc]init];
_scanApiOpen=FALSE;
_scanApiTerminated=TRUE;// by default ScanApi is not started
+ _shared=NO;
+ _delegateStack=[[NSMutableArray alloc]init];
}
return self;
}
@@ -130,6 +134,7 @@
_commandContextsLock=nil;
_deviceInfoList=nil;
+ _delegateStack=nil;
}
#else
-(void)dealloc{
@@ -151,10 +156,54 @@
[_deviceInfoList release];
_deviceInfoList=nil;
+
+ [_delegateStack release];
+ _delegateStack=nil;
+
[super dealloc];
}
#endif
++(ScanApiHelper*)sharedScanApiHelper{
+ static ScanApiHelper* scanApiHelper=nil;
+ if(scanApiHelper==nil){
+ scanApiHelper=[[ScanApiHelper alloc]init];
+ scanApiHelper->_shared=YES;
+ }
+ return scanApiHelper;
+}
+
+-(void)pushDelegate:(id<ScanApiHelperDelegate>)delegate{
+ if(_delegate != delegate){
+ if(_delegate!=nil){
+ [_delegateStack addObject:_delegate];
+ }
+ _delegate=delegate;
+ [self generateDeviceArrivals];
+ }
+}
+
+-(void)popDelegate:(id<ScanApiHelperDelegate>)delegate{
+ if(_delegate ==delegate){
+ if(_delegateStack.count>0){
+ id<ScanApiHelperDelegate> newDelegate=[_delegateStack objectAtIndex:_delegateStack.count-1];
+ [_delegateStack removeLastObject];
+ _delegate = newDelegate;
+ // generate a device Arrival for each scanner we've already receive
+ // so that the new view can be aware of the connected scanners
+ if(_delegate!=nil){
+ for (NSString* key in _deviceInfoList) {
+ DeviceInfo* device=[_deviceInfoList objectForKey:key];
+ [_delegate onDeviceArrival:ESKT_NOERROR device:device];
+ }
+ }
+ }
+ else{
+ _delegate=nil;
+ }
+ }
+}
+
/**
* register for notifications in order to receive notifications such as
* "Device Arrival", "Device Removal", "Decoded Data"...etc...
@@ -1519,14 +1568,15 @@
#endif
// release the previous ScanAPI object instance if
// it exists
- if(_scanApi!=nil){
- [_scanApi close];
- [SktClassFactory releaseScanApiInstance:_scanApi];
- }
_scanApi=[SktClassFactory createScanApiInstance];
SKTRESULT result=[_scanApi open:nil];
- if(_delegate!=nil)
- [_delegate onScanApiInitializeComplete:result];
+ if((_delegate!=nil)&&([_delegate respondsToSelector:@selector(onScanApiInitializeComplete:)])){
+ [_delegate onScanApiInitializeComplete:result];
+ }
_scanApiTerminated=FALSE;
#if __has_feature(objc_arc)
@@ -1576,8 +1626,9 @@
}
}
else{
- if(_delegate!=nil)
+ if((_delegate!=nil)&&([_delegate respondsToSelector:@selector(onErrorRetrievingScanObject:)])){
[_delegate onErrorRetrievingScanObject:result];
+ }
}
}
return result;
@@ -1600,8 +1651,9 @@
result=[self handleDeviceRemoval:scanObj];
break;
case kSktScanMsgIdTerminate:
- if(_delegate!=nil)
+ if((_delegate!=nil)&&([_delegate respondsToSelector:@selector(onScanApiTerminated)])){
[_delegate onScanApiTerminated];
+ }
closeScanApi=TRUE;
break;
case kSktScanMsgSetComplete:
@@ -1619,8 +1671,9 @@
// if there is an error then report it to the ScanAPIHelper user
if(!SKTSUCCESS(result)){
- if(_delegate!=nil)
+ if((_delegate!=nil)&&([_delegate respondsToSelector:@selector(onError:)])){
[_delegate onError:result];
+ }
}
return closeScanApi;
}
@@ -1657,8 +1710,9 @@
}
// notify the ScanApiHelper user a scanner has connected to this host
- if(_delegate!=nil)
+ if((_delegate!=nil)&&([_delegate respondsToSelector:@selector(onDeviceArrival:device:)])){
[_delegate onDeviceArrival:result device:deviceInfo];
+ }
#if __has_feature(objc_arc)
#else
@@ -1702,8 +1756,9 @@
[SktClassFactory releaseDeviceInstance:scanDevice];
// notify the ScanApiHelper user a scanner has connected to this host
- if(_delegate!=nil)
+ if((_delegate!=nil)&&([_delegate respondsToSelector:@selector(onDeviceRemoval:)])){
[_delegate onDeviceRemoval:deviceInfo];
+ }
return result;
}
@@ -1766,7 +1821,7 @@
result=[self handleDecodedData:scanObj];
break;
case kSktScanEventError:
- if(_delegate!=nil)
+ if((_delegate!=nil)&&([_delegate respondsToSelector:@selector(onError:)]))
[_delegate onError:[[scanObj Msg]Result]];
break;
@@ -1809,6 +1864,7 @@
return result;
}
/**
* sendNextCommand
*
@@ -1855,4 +1911,25 @@
return result;
}
+/**
+ * generateDeviceArrivals
+ *
+ * internal function to generate device
+ * arrival notifications when the delegate
+ * change to a new delegate.
+ * This is used in a multi views application
+ * that has more than one view to receive the
+ * decoded data
+ */
+-(void)generateDeviceArrivals{
+ // generate a device Arrival for each scanner we've already receive
+ // so that the new view can be aware of the connected scanners
+ if(_delegate!=nil){
+ for (NSString* key in _deviceInfoList) {
+ DeviceInfo* device=[_deviceInfoList objectForKey:key];
+ [_delegate onDeviceArrival:ESKT_NOERROR device:device];
+ }
+ }
+}
+
@end
关于Swift 委托(delegate)和 SocketScan scanApi.setDelegate(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27340239/
我最近刚刚实现了这个扫描 API,但遇到了一些键盘问题。除非我选择了该文本字段,否则条形码不会填充到所需的 UITextField 对象 中。此外,当扫描仪连接到我的设备时,键盘没有出现在屏幕上,如果
我在 iOS 中实现了 socket scan API 来扫描并获取条形码,这似乎工作正常。我想知道是否有办法通过 socket scan API 找出运营商名称? 最佳答案 ScanAPI 仅提供条
我每次都得到 100% 的结果...为什么(iOS/SocketScan API v.10.2.227)?这是我的代码: -(void) onGetBatteryInfo:(ISktScanObjec
无论我尝试什么,我目前都无法从 iOS/SocketScan API 取回电池电量。我使用的是 10.3.36 版,这是我目前的代码: func onDeviceArrival(result: SKT
SocketMobile 的开发者门户似乎不可用,有没有人有关于如何使用 Swift 实现软扫描功能(使用 iPhone 相机)的最小示例? 我有一个适用于物理扫描仪的 iOS 应用程序,但我想扩展它
适用于 iOS 的 SocketScan API 是用 Objective-C 编写的,旨在将 CHS 模式下的 SocketMobile 扫描器集成到 native 应用程序中。 启动 ScanAp
我正在尝试将 SocketScan SDK 与用 Swift 编写的应用程序一起使用。使用桥接 header ,可以轻松导入 Objective-C 代码。但是,在运行该应用程序时,它会在 onDev
我是一名优秀的程序员,十分优秀!