【发布时间】:2021-08-08 10:14:31
【问题描述】:
我面临的问题是我想将以下框架集成到一个 SwiftUI 项目中,即:ACSSmartCardIO & SmartCardIO
我不精通 SwiftUI 或 iOS 开发,无法理解如何实现委托并在 SwiftUI 中使用它。
文档显示: 在连接蓝牙卡终端之前,您的应用必须导入 ACSSmartCardIO 模块。您可以从 BluetoothSmartCard.shared 对象获取蓝牙终端管理器和终端工厂的实例。要从 BluetoothTerminalManager 对象接收事件,您的应用必须为其分配一个委托对象。
...
import SmartCardIO
import ACSSmartCardIO
...
class ViewController: UIViewController {
...
let manager = BluetoothSmartCard.shared.manager
let factory = BluetoothSmartCard.shared.factory
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
...
// Set the delegate.
manager.delegate = self
...
}
...
}
...
您的应用必须检查来自 CentralManager 对象的状态,并在不支持蓝牙或您的应用无法在 iOS 设备上使用蓝牙的其他原因时显示消息。
// MARK: - BluetoothTerminalManagerDelegate
extension ViewController: BluetoothTerminalManagerDelegate {
func bluetoothTerminalManagerDidUpdateState(_ manager: BluetoothTerminalManager) {
var message = ""
switch manager.centralManager.state {
case .unknown, .resetting:
message = "The update is being started. Please wait until Bluetooth is ready."
case .unsupported:
message = "This device does not support Bluetooth low energy."
case .unauthorized:
message = "This app is not authorized to use Bluetooth low energy."
case .poweredOff:
if !firstRun {
message = "You must turn on Bluetooth in Settings in order to use the reader."
}
default:
break
}
if !message.isEmpty {
// TODO: Show the message.
// ...
}
firstRun = false
}
...
}
要查找 BLE 卡终端,您必须使用 BluetoothTerminalManager 对象中的 BluetoothTerminalManager.startScan(terminalType:) 方法。
...
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let identifier = segue.identifier {
switch identifier {
case "ScanTerminals":
...
// Start the scan.
manager.startScan(terminalType: .amr220c)
...
default:
break
}
}
}
...
【问题讨论】:
标签: ios swift swiftui ios-frameworks