Links

iOS SDK

Overview

Cardknox iOS SDK is a mobile SDK targeted towards Objective C / Swift mobile application developers; it allows developers to process transactions with the Cardknox Transactions API.

Getting started

To start, download the SDK framework file:
Integrate the framework file into your XCode project by referring to the technical documentation.

Choose your integration path

The SDK offers developers a couple of ways to process transactions:
  • In scope function
  • Out of scope function
  • Custom UI set of functions

Out of scope

Use the out of scope function when the user needs to provide their credit card information. This function displays the SDK user interface, effectively giving the control over to the SDK to acquire the sensitive credit card data from the user. The user provides the sensitive information either via a form or via a credit card device, and then the SDK processes the transaction with the gateway.

In scope

Use the in scope function when there is no need for the SDK to interact with the user through a user interface. The developer should either pass in a card number + an expiration date, or provide a tokenized card data via the xToken parameter to this function to quickly process the transaction and retrieve back the results.

Custom UI

Custom-UI integration consists of a set of functions to control the card reader device via the SDK. Currently supported card reader device is a Bluetooth VP3300 card reader. This integration path is useful when the developer has an existing UI and wishes to use a card reader device to obtain users' card sensitive information and then process the transaction with the gateway. The SDK offers a set of functions to control the card reader device. The SDK takes care of processing with the gateway and notifying the Developer’s application with the processing results.
The SDK offers the following functions: “start scanning for devices”, “stop scanning for devices”, “connect to device”, “disconnect from device”, “start transaction”, “stop transaction”.

Transaction workflows

Click here to view the transaction workflows online.
Download our sample applications:

Basic parameters

Name
Required
Description
Sample data
xKey
Yes
Cardknox account key
CardknoxSupport_Test
xVersion
Yes
Gateway version
4.5.9
xSoftwareName
Yes
Name of your software
My app
xSoftwareVersion
Yes
Version number of your software
1.0.0

Basic parameter functions

Prior to any processing, the Cardknox SDK needs to be configured with user’s metadata and the account key. These functions can be called anywhere in the application any number of times to change the metadata and/or current account key.
Function
Description
setxSoftwareName
Specifies the name of your software (xSoftwareName), version number of your software (xSoftwareVersion) and the gateway version (xVersion)
Sample data (Swift)
CardknoxSDK.setxSoftwareName("Your app name", xSoftwareVersion: "Your app version, such as 1.0.0", xVersion: "4.5.9");
Sample data (Objective C)
[CardknoxSDK setxSoftwareName:@"Your app name" xSoftwareVersion:@"Your app version, such as 1.0.0" xVersion:@"4.5.9"];
setPrincipalKey
Accepts the xKey.
xKey is a Cardknox account key.
All transactions being processed after calling this function will be associated to the account that was passed in into the function.
Sample data (Swift)
CardknoxSDK.setPrincipalKey("Your xKey value")
Sample data (Objective C)
[CardknoxSDK setPrincipalKey:@"Your xKey value"];

Transaction required parameters

Each integration path has a “process” function that accepts a “transaction parameters” object. Developers specify required values for transaction processing through that object. Same object can be used to specify optional parameters to associate with a transaction; such as invoice numbers, billing address, etc.
Name
Description
Sample data
xCommand
Cardknox transaction type
cc:sale

Transaction optional parameters

Optional transaction parameters further complement the transaction processing. All the parameters are being sent to the Gateway during processing.
Name
Description
Sample data
VP3300TransactionTimeout
Start transaction timeout, in seconds.
Once the transaction with the VP3300 reader is started; the SDK times out if the card is not provided in the time frame specified by this timeout value.
This parameter is only applicable for SDK integrations that use a VP3300 card reader; such as CustomUI or OutOfScope integrations.
Refer to the Custom UI flow or Out Of Scope flow for samples.
xCardNum
Card number
4444333322221111
xExpDate
Card expiration date.
Expected format is MMYY where MM is a month, and YY are last 2 year digits
“1126” for (November 2026)
“0122” for “January 2022”
xToken
The Cardknox token that references a previously used payment method. When using a token, xCardNum xExp and xMagstripe should not be used.
61h72mmh68phn9q233634ph3g54p1499m69qhp4816pn528h84
xRefNum
Used to reference a previous transaction when doing a follow-up transaction, typically a refund, void, or capture.
12345678
xInvoice
The merchant’s invoice number for the transaction. xInvoice is recommended when available for improved duplicate handling.
The merchant’s invoice number for the transaction. xInvoice is recommended when available for improved duplicate handling.
xPoNum
The merchant’s purchase order number for the transaction.
123485
xDescription
This is a description
This is a description
xName
The cardholder’s name.
John Doe
xBillFirstName
The customer’s first name for their billing profile.
John
xBillLastName
The customer’s last/family name for their billing profile.
Doe
xBillCompany
The customer’s company name for their billing profile.
Acme
xBillStreet
The customer’s street address for their billing profile.
123 Any Street
xBillStreet2
The customer’s second street address for their billing profile.
123 Any Street
xBillCity
The customer’s city for their billing profile.
Anytown
xBillState
The customer’s state for their billing profile.
NY
xBillZip
The customer’s zip code for their billing profile.
12345
xBillCountry
The customer’s phone number for their billing profile.
USA
xBillPhone
The customer’s phone number for their billing profile.
8005551212
xShipFirstName
The customer’s last/family name for their shipping profile.
John
xShipLastName
The customer’s last/family name for their shipping profile.
Doe
xShipCompany
The customer’s company name for their shipping profile.
Acme
xShipStreet
The customer’s street address for their shipping profile.
123 Any Street
xShipStreet2
The customer’s second street address for their shipping profile.
123 Any Street
xShipCity
The customer’s city for their shipping profile.
Anytown
xShipState
The customer’s state for their shipping profile.
NY
xShipZip
The customer’s zip code for their shipping profile.
12345
xShipCountry
The customer’s country for their shipping profile.
USA
xShipPhone
The customer’s phone number for their shipping profile.
1113333444
xCustom01
Custom field 1. Use this for any additional data
xCustom02
Custom field 2. Use this for any additional data
xCustom03
Custom field 3. Use this for any additional data
xAuthCode
xAuthCode is a verification number provided by the issuing bank to be used with the cc:postauth command.
xAllowDuplicate
By default, Cardknox rejects duplicate transactions within 10 minutes of the original transaction. This command overrides that safeguard. True/False allowed.
False
xCustReceipt
True/False value indicating if the email address specified in xEmail should receive a receipt containing the transaction details.
False
xEmail
The customer’s email address.

Retrieving results with callbacks

The SDK can notify the application about various events during processing, such as about different card reader events during out of scope processing, or perhaps about a completed bluetooth device scan during custom UI processing.
Developers opt in to receive callbacks by subscribing to the NSNotificationCenter using one of its' methods, using a predefined value from the SDK for the “name” parameter.
The SDK uses the same “name” value to report results & various information back to subscribers.
Available callback types and integrations where they are applicable in are as follows:
Callback type
Description
Available in integration
Transaction result
The SDK processes a payment with the Gateway and delivers an object containing all processed transaction details.
Out of scope
Custom UI
Card reader event
The SDK keeps informing the subscribers about various events taking place between the application and the card reader device; such as “connected”, “disconnected”, “transaction started”, “on bluetooth disabled error”, etc.
Out of scope
Custom UI
Scanned bluetooth device
Trigger a bluetooth device scanning process with the “start scanning” function.
During the scanning process, the SDK keeps informing subscribers with objects that encapsulate all the information about each newly discovered Bluetooth device.
The SDK expects one of the objects as an argument in the “connect to device” function.
Custom UI
Scan completed
Trigger a bluetooth device scanning process with the “start scanning” function.
Stop the process with a “stop scanning” function.
Once the scanning process is stopped, the SDK reports back a list of all scanned bluetooth devices.
The SDK expects one of the objects as an argument in the “connect to device” function.
Custom UI

Callback subscriptions & result handling

Based on your integration path choice, choose an available callback type for that integration path and subscribe with the NSNotificationCenter to receive appropriate information back from the SDK.

Transaction result callback subscription

This callback delivers a “transaction processed response” object to the subscriber.
For Swift applications using Swift UI, subscription can be made in a View as follows:
import SwiftUI
struct ProcessOutOfScopeView: View
{
// Define a Publisher
let transactionPublisher =
NotificationCenter.default.publisher(for: NSNotification.Name(CardknoxSDK.transactionResultSubscription_NSNotificationCenterName()))
var body: some View
{
VStack{}
// Subscribe the Publisher with the NotificationCenter
.onReceive(transactionPublisher, perform: transactionNotification)
}
// Define a function that accepts a Notification.
// This method will be invoked when the SDK sends transaction processing results
func transactionNotification(notif: Notification) {
// Use the SDK's "response" object utility method to transform a Notification into a "response" object
let response = PaymentTransactionResponse.unwrap(notif) as! PaymentTransactionResponse
if response.isSuccess()
{
// Transaction successfully processed
let errorMessage = response.errorMessage()!
let refNum = response.xRefNum()!
// ... other properties ...
} else {
// Transaction processing resulted in an error message which can be extracted from this property:
let errorMessage = response.errorMessage()!
let errorCode = response.xErrorCode()!
let error = response.xError()!
}
}
}
For Swift and Objective-C applications using Storyboard, subscriptions can be made in the appropriate UIViewController lifecycle method:
Swift
Objective-C
1
import UIKit
2
3
class ViewController: UIViewController {
4
5
override func viewWillAppear(_ animated: Bool) {
6
super.viewWillAppear(animated);
7
let _notificationCenter = NotificationCenter.default
8
_notificationCenter.addObserver(self,
9
selector: #selector(transactionNotification(aNotification:)),
10
name: Notification.Name(CardknoxSDK.transactionResultSubscription_NSNotificationCenterName()),
11
object: nil)
12
}
13
14
// Define a function that accepts a Notification.
15
// This method will be invoked when the SDK sends transaction processing results
16
@objc func transactionNotification(aNotification : Notification) {
17
// Use the SDK's "response" object utility method to transform a Notification into a "response" object
18
let response = PaymentTransactionResponse.unwrap(aNotification) as! PaymentTransactionResponse
19
20
if response.isSuccess()
21
{
22
// Transaction successfully processed
23
let errorMessage = response.errorMessage()!
24
let refNum = response.xRefNum()!
25
// ... other properties ...
26
} else {
27
// Transaction processing resulted in an error message which can be extracted from this property:
28
let errorMessage = response.errorMessage()!
29
let errorCode = response.xErrorCode()!
30
let error = response.xError()!
31
}
32
}
33
}
#import "ViewController.h"
#import "CardknoxPaymentsSDK/CardknoxPaymentsSDK.h"
@implementation ViewController
-(void)viewWillAppear:(BOOL)animated
{
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(transactionNotification:)
name:[CardknoxSDK transactionResultSubscription_NSNotificationCenterName]
object:nil];
}
// Define a function that accepts a Notification.
// This method will be invoked when the SDK sends transaction processing results
-(void)transactionNotification:(NSNotification*)aNotification{
// Use the SDK's "response" object utility method to transform a Notification into a "response" object
PaymentTransactionResponse * response = [PaymentTransactionResponse unwrap:aNotification];
if(response.isSuccess)
{
// Transaction successfully processed
NSString * errorMessage = response.errorMessage;
NSString * refNum = response.xRefNum;
// ... other properties ...
NSLog([NSString stringWithFormat:@"Transaction success response! Ref num: %@", refNum]);
}
else
{
// Transaction processing resulted in an error; message can be extracted from this property:
NSString * errorMessage = response.errorMessage;
NSString * errorCode = response.xErrorCode;
NSString * error = response.xError;
NSLog([NSString stringWithFormat:@"Transaction error response - %@", errorMessage]);
}
}
@end

Card reader event callback subscription

This callback delivers information about various events happening between the application & the card reader.
For example, while out-of-scope processing the SDK can report back error events related to bluetooth device pairing, such as “bluetooth not turned on” to indicate that the mobile device wanted to use the bluetooth service to find a near card reader device but the service is unavailable, or an error such as “waiting for device bluetooth response” to indicate that the mobile device found an eligible bluetooth card reader device, and is expecting the card reader to respond back with bluetooth data. This could mean that the bluetooth button on the card reader needs to be pressed.
After an established bluetooth pair, the SDK reports back events related to obtaining the card data via the card reader. For example, a “connected” event means that the mobile device & the card reader are connected and a card data transaction can start. A “transaction started” event means that the SDK initiated a card data transaction with the card reader and the physical card can be tapped onto the card reader.
For Swift applications using Swift UI, subscription can be made in a View as follows:
import SwiftUI
struct ProcessOutOfScopeView: View
{
// Define a Publisher
let cardReaderEventPublisher =
NotificationCenter.default.publisher(for: NSNotification.Name(CardknoxSDK.cardreaderEventSubscription_NSNotificationCenterName()))
var body: some View
{
VStack{}
// Subscribe the Publisher with the NotificationCenter
.onReceive(cardReaderEventPublisher, perform: cardReaderEventNotification)
}
// Define a function that accepts a Notification.
// This method will be invoked when the SDK sends card reader events
func cardReaderEventNotification(aNotification: Notification){
// Use the SDK's "response" object utility method to transform a Notification into a "response" object
let callback = CardknoxCardReaderCallback.unwrap(aNotification) as! CardknoxCardReaderCallback
// Read the event code
let code : Int32 = callback.code();
// Read the event name
let name: String = callback.name()!;
NSLog(String(format: "Card reader - %@", name));
// Match the non-error code
if(code == CardknoxCardReaderCallbackType.connected())
{
NSLog("Connected!");
}
// Match the error code & get the message
if(code == CardknoxCardReaderCallbackType.error())
{
let errorMessage : String = callback.message()!;
NSLog(String(format: "Card reader - %@", errorMessage));
}
}
}
For Swift and Objective-C applications using Storyboard, subscriptions can be made in the appropriate UIViewController lifecycle method:
Swift
Objective-C
import UIKit
class ViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated);
let _notificationCenter = NotificationCenter.default
_notificationCenter.addObserver(self,
selector: #selector(cardReaderEventNotification(aNotification:)),
name: Notification.Name(CardknoxSDK.cardreaderEventSubscription_NSNotificationCenterName()),
object: nil)
}
// Define a function that accepts a Notification.
// This method will be invoked when the SDK sends card reader events
@objc func cardReaderEventNotification(aNotification : Notification) {
// Use the SDK's "response" object utility method to transform a Notification into a "response" object
let callback = CardknoxCardReaderCallback.unwrap(aNotification) as! CardknoxCardReaderCallback
// Read the event code
let code : Int32 = callback.code();
// Read the event name
let name: String = callback.name()!;
NSLog(String(format: "Card reader - %@", name));
// Match the non-error code
if(code == CardknoxCardReaderCallbackType.connected())
{
NSLog("Connected!");
}
// Match the error code & get the message
if(code == CardknoxCardReaderCallbackType.error())
{
let errorMessage : String = callback.message()!;
NSLog(String(format: "Card reader - %@", errorMessage));
}
}
}
#import "ViewController.h"
#import "CardknoxPaymentsSDK/CardknoxPaymentsSDK.h"
@implementation ViewController
-(void)viewWillAppear:(BOOL)animated
{
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(cardReaderEventNotification:)
name:[CardknoxSDK cardreaderEventSubscription_NSNotificationCenterName]
object:nil];
}
// Define a function that accepts a Notification.
// This method will be invoked when the SDK sends card reader events
-(void)cardReaderEventNotification:(NSNotification*)callbackNotification{
// Use the SDK's "response" object utility method to transform a Notification into a "response" object
CardknoxCardReaderCallback* callback = [CardknoxCardReaderCallback unwrap:callbackNotification];
// Read the event code
int code = callback.code;
// Read the event name
NSString* name = callback.name;
NSLog([NSString stringWithFormat:@"Card reader - %@", name]);
// Match the non-error code
if(code == CardknoxCardReaderCallbackType.connected){
NSLog(@"Connected");
}
// Match the error code & get the message
if(code == CardknoxCardReaderCallbackType.error){
NSString* errorMessage = callback.message;
NSLog([NSString stringWithFormat:@"Card reader - %@", errorMessage]);
}
}
@end

Card reader events

When a card reader event happens, the SDK delivers an object, of a type named similarly to “CardknoxCardReaderCallback”, back into the app.
The object encapsulates two things:
  • an event integer code
  • an event name; such as “connected”, “disconnected”, etc.
Event integer codes are enumerated in a type named similarly to "CardknoxCardReaderCallbackType".
Developer can match the received integer code value with the enumeration of interest to pinpoint a wanted event.
Event name
Description
connecting
The Cardknox SDK calls into the card reader SDK to connect with the reader, in scenarios such as:
  • “connect to device” method in the “custom ui” integration
  • when it presents its own UI in “process out of scope” integration
The card reader SDK starts searching for nearby IDTech card readers & notifies the Cardknox SDK with a “connecting” event.
The Cardknox SDK raises a “connecting” callback which notifies the app.
waitingForDeviceResponse
This event is raised whenever the Cardknox SDK scans for nearby card readers. Awake readers are usually auto connected straight away.
Asleep or not, this event is always raised from the SDK to indicate that some action is required on the device.
This event is raised in the “custom ui” integration when:
  • a Developer calls the “start scanning” method and the underlying bluetooth adapter starts scanning
  • or when a Developer calls the “connect to device” method. Cardknox SDK will call the card reader SDk to “connect” and raise this event
This event is raised in the “process out of scope” integration when:
  • the Cardknox SDK presents its user interface; automatically starting a bluetooth scan for a nearby card reader, expecting a response back
connected
Raised when the app & the card reader establish a bluetooth connection.
Bluetooth connection is established when the Cardknox SDK scans for card readers, and card readers respond back with some bluetooth payload.
disconnected
Card reader SDK sends this event to the Cardknox SDK whenever a bluetooth card reader goes to sleep.
This event is never raised explicitly by the Cardknox SDK.
transactionStarting
Informational event.
Cardknox SDK raises this event before it places a call to the underlying card reader SDK in order to start a transaction with the card reader.
transactionStarted
Cardknox SDK attempts a “start transaction” call with the card reader SDK.
Card reader SDK successfully starts the transaction and notifies the Cardknox SDK with this event.
Cardknox SDK calls back with this event to the app.
transaction start error timeout
Cardknox SDK attempts a “start transaction” call with the card reader SDK.
The card reader never receives a card in a specified timeout frame window (about 10 seconds) and the transaction times out.
transaction start device disconnected
Cardknox SDK wants to start the transaction with a disconnected device.
transactionCancelled
Generally, this is raised whenever an error occurs at any point between starting a transaction with the reader and obtaining the card data.
See the “error” event for error examples.
The Cardknox SDK in cases of these errors preventively calls the “cancel any pending transactions” internally to clean up.
Specifically, this event is raised in the “custom ui” integration when:
  • a “cancel transaction” method is called & the card reader SDK cancels all pending transactions between the app & the card reader
Furthermore, this event is raised in the “process out of scope” integration when:
  • the Cardknox SDK’s user interface is closed; either via a pull down gesture or via a “close” UI element; to clean things up
  • the charge amount gets modified via UI and becomes invalid, effectively invalidating any pending transactions between the app & the card reader
scanStop
Indicates that the Cardknox SDK stopped its own bluetooth scanning.
Raised in the “custom ui” integration when:
  • a Developer explicitly calls the “stop scanning” method
  • when the “start scanning” method was called with a timeout value and the timeout expired or was cancelled / interrupted
  • in the “disconnect from current device” call if the “connect to device” method was called beforehand but there is no connected device to disconnect from
Raised in the “process out of scope” integration when:
  • the SDK shows its' UI and automatically starts scanning for nearby readers. User can explicitly stop the scanning process
scanStart
Indicates that the Cardknox SDK started a bluetooth scanning process.
Raised in the “custom ui” integration when:
  • the “start scanning” method was called, with or without a timeout value
Raised in the “process out of scope” integration when:
  • the SDK shows its' UI and automatically starts scanning for nearby readers
error
This is raised whenever an error occurs at any point between starting a transaction with the reader and obtaining the card data.
Things that might go wrong (not a complete list) are:
  • transaction with the card reader failed to start
  • emv card readings are corrupted
  • transaction started but the card was not tapped / swiped / inserted in time
An descriptive error message is always accompanying this event.

Scanned bluetooth device callback subscription

One of the Custom UI integration functions is a “start scanning” function. The function keeps scanning for nearby bluetooth devices until it is manually stopped with the “stop scanning” function or if it times out.
During the scanning process, for every scanned device the SDK sends a “scanned device” object that contains all the necessary metadata about the scanned device, such as the devices' display name or its internal name.
For Swift applications using Swift UI, subscription can be made in a View as follows:
1
import SwiftUI
2
3
struct CustomUIView: View
4
{
5
// Track a scanned device
6
@State private var scannedDevice : CardknoxSDKCustomUIScannedDevice!;
7
8
// Define a Publisher
9
let customUIscannedDeviceSubscription =
10
NotificationCenter.default.publisher(for: NSNotification.Name(CardknoxSDKCustomUI.customUI_scannedDevice_Subscription_NSNotificationCenterName()))
11
12
var body: some View
13
{
14
VStack{}
15
// Subscribe the Publisher with the NotificationCenter
16
.onReceive(customUIscannedDeviceSubscription, perform: customUIscannedDeviceSubscription)
17
}
18
19
// Define a function that accepts a Notification.
20
// This method will be invoked when the SDK sends scanned device information
21
func customUIscannedDeviceSubscription(aNotification: Notification){
22
// Use the SDK's "response" object utility method to transform a Notification into a "response" object
23
scannedDevice = CardknoxSDKCustomUIScannedDevice.from(aNotification) as! CardknoxSDKCustomUIScannedDevice;
24
let name = scannedDevice.name();
25
let displayName = scannedDevice.displayName();
26
let uuid = scannedDevice.uuid();
27
}
28
}
For Swift and Objective-C applications using Storyboard, subscriptions can be made in the appropriate UIViewController lifecycle method:
Swift
Objective-C
1
import UIKit
2
3
class ViewController: UIViewController {
4
// Track a scanned device
5
private var scannedDevice : CardknoxSDKCustomUIScannedDevice!;
6
7
override func viewWillAppear(_ animated: Bool) {
8
super.viewWillAppear(animated);
9
let notificationCenter = NotificationCenter.default
10
notificationCenter.addObserver(self, selector: #selector(customUIScannedDeviceSubscription(aNotification:)), name: Notification.Name( CardknoxSDKCustomUI.customUI_scannedDevice_Subscription_NSNotificationCenterName()), object: nil)
11
}
12
13
// Define a function that accepts a Notification.
14
// This method will be invoked when the SDK sends scanned device information
15
@objc func scannedDeviceNotification(aNotification : Notification) {
16
// Use the SDK's "response" object utility method to transform a Notification into a "response" object
17
let scannedDevice = CardknoxSDKCustomUIScannedDevice.from(aNotification) as! CardknoxSDKCustomUIScannedDevice;
18
let name = scannedDevice.name();
19
let displayName = scannedDevice.displayName();
20
let uuid = scannedDevice.uuid();
21
22
// Uuid can be used to connect to the device, like this:
23
let exampleCustomUI: CardknoxSDKCustomUI? = nil;
24
exampleCustomUI?.connect(withUUID: uuid);
25
}
26
}
1
#import "ViewController.h"
2
#import "CardknoxPaymentsSDK/CardknoxPaymentsSDK.h"
3
4
@interface ViewController ()
5
{
6
CardknoxSDKCustomUIScannedDevice* scannedDevice;
7
}
8
@end
9
10
@implementation ViewController
11
12
-(void)viewWillAppear:(BOOL)animated
13
{
14
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
15
16
[notificationCenter addObserver:self
17
selector:@selector(customUIScannedDeviceSubscription:)
18
name:[CardknoxSDKCustomUI customUI_scannedDevice_Subscription_NSNotificationCenterName]
19
object:nil];
20
}
21
22
// Define a function that accepts a Notification.
23
// This method will be invoked when the SDK sends scanned device information
24
-(void)customUIScannedDeviceSubscription:(NSNotification*)notification{
25
CardknoxSDKCustomUIScannedDevice* scannedDevice = [CardknoxSDKCustomUIScannedDevice from:notification];
26
NSString* name = [scannedDevice name];
27
NSString* displayName = [scannedDevice displayName];
28
NSString* uuid = [scannedDevice uuid];
29
}
30
31
@end

Scan completed callback subscription

One of the Custom UI integration functions is a “start scanning” function. The function keeps scanning for nearby bluetooth devices until it is manually stopped with the “stop scanning” function or if it times out.
Once the scanning process ends, the SDK sends a list of scanned device objects to all subscribers. Any object in the retrieved list can be used as an argument to the “connect to device” method.
For Swift applications using Swift UI, subscription can be made in a View as follows:
1
import SwiftUI
2
3
struct CustomUIView: View
4
{
5
// Track a scanned device
6
@State private var scannedDevice : CardknoxSDKCustomUIScannedDevice!;
7
8
// Define a Publisher
9
let customUIscanCompletedSubscription =
10
NotificationCenter.default.publisher(for: NSNotification.Name(CardknoxSDKCustomUI.customUI_scanCompleted_Subscription_NSNotificationCenterName()))
11
12
var body: some View
13
{
14
VStack{}
15
// Subscribe the Publisher with the NotificationCenter
16
.onReceive(customUIscanCompletedSubscription, perform: customUIscanCompletedNotification(aNotification:))
17
}
18
19
// Define a function that accepts a Notification.
20
// This method will be invoked when the SDK stops the scanning process and sends back all scanned devices
21
func customUIscanCompletedNotification(aNotification: Notification){
22
let scanCompleted = CardknoxSDKCustomUIScanCompleted.from(aNotification) as! CardknoxSDKCustomUIScanCompleted;
23
let devices : Array<Any> = scanCompleted.scannedDevices();
24
25
// Example to get a first scanned VP3300 device.
26
// All VP3300 devices have their internal name start with IDTECH prefix
27
if(devices != nil && devices.count > 0)
28
{
29
for dev in devices{
30
let typedDevice = dev as! CardknoxSDKCustomUIScannedDevice
31
let name = typedDevice.name()!;
32
33
if name.hasPrefix("IDTECH")
34
{
35
// Save the reference & use it with the "connect to device" method
36
scannedDevice = typedDevice;
37
break;
38
}
39
}
40
}
41
}
42
}
For Swift applications using Storyboard, subscriptions can be made in the appropriate UIViewController lifecycle method. For Objective C applications, subscriptions can be made in the appropriate UIViewController lifecycle method:
Swift
Objective-C
1
import UIKit
2
3
class ViewController: UIViewController {
4
5
override func viewWillAppear(_ animated: Bool) {
6
super.viewWillAppear(animated);
7
let notificationCenter = NotificationCenter.default
8
notificationCenter.addObserver(self, selector: #selector(customUIScanCompletedSubscription(aNotification:)), name: Notification.Name( CardknoxSDKCustomUI.customUI_scanCompleted_Subscription_NSNotificationCenterName()), object: nil)
9
}
10
11
// Define a function that accepts a Notification.
12
// This method will be invoked when the SDK stops the scanning process and sends back all scanned devices
13
@objc func scanCompletedNotification(aNotification: Notification){
14
let scanCompleted = CardknoxSDKCustomUIScanCompleted.from(aNotification) as! CardknoxSDKCustomUIScanCompleted;
15
let devices : Array<Any> = scanCompleted.scannedDevices();
16
var scannedDevice: CardknoxSDKCustomUIScannedDevice? = nil;
17
18
// Example to get a first scanned VP3300 device.
19
// All VP3300 devices have their internal name start with IDTECH prefix
20
if(devices != nil && devices.count > 0)
21
{
22
for dev in devices{
23
let typedDevice = dev as! CardknoxSDKCustomUIScannedDevice
24
let name = typedDevice.name()!;
25
26
if name.hasPrefix("IDTECH")
27
{
28
// Save the reference & use it with the "connect to device" method
29
scannedDevice = typedDevice;
30
break;
31
}
32
}
33
}
34
}
35
}