SCMEnvironment
public final class SCMEnvironment
Main class for the Smart Card Middleware API.
This class provides the initial entry points for connecting to the iOS API, obtaining the readers and listening to events.
-
Initializes the list of all smart card readers attached to the device (through
getReaders()) and starts monitoring smart card reader events.Example:
var environment: SCMEnvironment? = nil group = DispatchGroup() group.enter() DispatchQueue.global(qos: .background).async { SCMEnvironment.createEnvironment { env, error in if let error = error { // fail to create the environment } else { environment = env } group.leave() } } group.wait() if let env = environment { env.addReaderListener(listener: self) }Note:
If the device supports NFC Tag Reading, a NFC
Readernamed"NFC Interface"will be added togetReaders()array.Declaration
Swift
public static func createEnvironment(completionHandler: @escaping (_ env: SCMEnvironment?, _ error: NSError?) -> Void)Parameters
completionHandlerThe completion handler block to be called as soon as the environment is created. It takes the following parameters:
- env
SCMEnvironment?: the newly createdSCMEnvironmentobject. - error :
nilif the environment creation is completed successfully; otherwise an error encapsulates the reason of failure.
Note
The completion handler is executed on the same type ofDispatchQueueas in the calling code. - env
-
Registers a reader event listener.
See also
Declaration
Swift
public func addReaderListener(listener: ReaderEvents)Parameters
listenerThe listener instance whose methods will be called when an event occurs.
-
Removes a reader event listener.
See also
Declaration
Swift
public func removeReaderEventListener(listener: ReaderEvents)Parameters
listenerThe listener instance to remove.
-
Returns an array of the bluetooth peripherals visible to the device.
Example:
do { let peripherals = try env.getDetectedBluetoothPeripherals() } catch SCMError.CXR_BT_INITIALIZATION_FAILED { // handle failed initialization of bluetooth } catch SCMError.CXR_BT_NOT_SCANNING { // start scan } catch SCMError.CXR_BT_ALREADY_PAIRED { // you can't use this function while being connected } catch { // other error }See also
Throws
A
SCMErrorrepresenting the occuring error.Declaration
Swift
public func getDetectedBluetoothPeripherals() throws -> Array<BluetoothPeripheral>Return Value
An array of
BluetoothPeripheralobjects. -
Returns the registered bluetooth peripheral.
See also
Declaration
Swift
public func getRegisteredBluetooth() -> BluetoothPeripheral?Return Value
A
BluetoothPeripheralobject representing the paired bluetooth device.nilif there are no paired bluetooth device. -
Gets if a bluetooth scan is running, for detecting bluetooth peripherals.
Declaration
Swift
public func isScanningBluetooth() -> BoolReturn Value
trueif a scan is currently running. -
Unregisters the paired bluetooth. This function also disconnects the current connected device.
Declaration
Swift
public func unregisterBluetooth()
-
Starts scanning for NFC tags. After calling this function, iOS will display an alert action sheet waiting to detect tags.
Example:
env.initTagReaderSession(message: "Present your smart card to the phone") { error in if let error = error { if error.scmError == SCMError.CXR_FUNCTION_TIMED_OUT { // the session has timed out } else if error.scmError == SCMError.CKR_FUNCTION_CANCELED { // the session was canceled by the user } else { // other failure reason } } else { // the session ended with a call to endTagReaderSession(errorMessage:String?) } )Declaration
Swift
public func initTagReaderSession(message: String?, completionHandler: @escaping (_ error: NSError?) -> Void)Parameters
messageDescriptive text message that is displayed on the alert action sheet once tag scanning has started. The string can be updated dynamically by calling
SCMEnvironment.updateTagReaderSession(message:).completionHandlerThe completion handler block to be called when the session ends. The Completion handler is called from background thread. It takes the following parameter:
- error
NSError?: If the operation has been ended explicitly by aSCMEnvironment.endTagReaderSession(errorMessage:)call,nilerror is passed. Check thescmErrorattribute of the error for more information.
- error
-
Closes the NFC Tag reader session and displays a feedback to the user.
Note:
If the session was not successful, a message must be provided, which will be displayed to the user as an error. If the operation was successful, the message must be set to
nil. In this case, a positive check mark will be displayed to the user.Declaration
Swift
public func endTagReaderSession(errorMessage: String?)Parameters
errorMessagenilif the session was successful, or a message error to display otherwise. -
Updates the text message that was displayed on the alert action sheet of the NFC Tag Reader Session.
Declaration
Swift
public func updateTagReaderSession(message: String)Parameters
messagethe new text message to be displayed on the alert action sheet.
-
Returns if the device supports NFC Tag Reading.
Declaration
Swift
public func isSupportedNfcTagReaderSession() -> BoolReturn Value
trueif the device supports NFC Tag Reader Session;falseotherwise.