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. See example above for common errorsDeclaration
Swift
public func getDetectedBluetoothPeripherals() throws -> Array<BluetoothPeripheral>Return Value
An array of
BluetoothPeripheralobjects. -
Registers a bluetooth peripheral.
Example:
let peripheral : BluetoothPeripheral //Obtained after getDetectedBluetoothPeripherals() do { let peripherals = try env.registerBluetooth(peripheral.getUuid()) } catch SCMError.CXR_BT_INITIALIZATION_FAILED { // handle failed initialization of bluetooth } catch SCMError.CXR_BT_NOT_SCANNING { // you must be scanning to register a bluetooth device } catch SCMError.CXR_BT_DEVICE_NOT_DETECTED { // the uuid does not correspond to any detected device. // This can occur if the device was turned off or went out of range between the call to getDetectedBluetoothPeripherals() and registerBluetooth() } catch SCMError.CXR_BT_DEVICE_NOT_SUPPORTED { // this device is not supported } catch { // other error }See also
SeeAlso:
SCMErrorThrows
A
SCMErrorrepresenting the occuring error. See example above for common errorsDeclaration
Swift
public func registerBluetooth(uuid: String, _ override: Bool = false) throwsParameters
uuidThe Universally Unique Identifier of the bluetooth peripheral.
-
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. -
Starts the scan for bluetooth devices.
Example:
do { let peripherals = try env.startBluetoothScan() } catch SCMError.CXR_BT_INITIALIZATION_FAILED { // handle failed initialization of bluetooth } catch SCMError.CXR_BT_ALREADY_PAIRED { // release connected reader before starting a new scan } catch { // other error }See also
Throws
A
SCMErrorrepresenting the occuring error. See example above for common errorsDeclaration
Swift
public func startBluetoothScan() throws -> BoolReturn Value
trueif scan could be started,falseif a scan is already started. -
Gets if a bluetooth scan is running, for detecting bluetooth peripherals.
Declaration
Swift
public func isScanningBluetooth() -> BoolReturn Value
trueif a scan is currently running. -
Stops scan for bluetooth peripherals.
Example:
do { let peripherals = try env.stopBluetoothScan() } catch SCMError.CXR_BT_INITIALIZATION_FAILED { // handle failed initialization of bluetooth } catch SCMError.CXR_BT_ALREADY_PAIRED { // release connected reader before starting a new scan } catch { // other error }See also
Throws
A
SCMErrorrepresenting the occuring error. See example above for common errorsDeclaration
Swift
public func stopBluetoothScan() throws -> BoolReturn Value
trueif scan is stopped,falseif it was already stopped. -
Disconnects the current bluetooth peripheral
Example:
do { let peripherals = try env.releaseBluetooth() } catch SCMError.CXR_BT_INITIALIZATION_FAILED { // handle failed initialization of bluetooth } catch { // other error }See also
Throws
A
SCMErrorrepresenting the occuring error. See example above for common errorsDeclaration
Swift
public func releaseBluetooth() throws -> BoolReturn Value
wether or not disconecting from the peripheral was successful.
-
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.
SCMEnvironment Class Reference