One Logo Pool Ball
CoreNFC · IOS API REFERENCE

CardSession

class · iOS 17.4+ · SDK 27.1

class CardSession

ISO7816 card emulation session. This is the session to allow ISO7816-4 protocol communication, send and receive Application Protocol Data Units (APDUs), with a NFC reader. To use this session: 1. Instantiate a new `CardSession` object. This will start the `eventStream`; `.sessionStarted` will be posted when the session becomes active. Once the session is started `Event.readerDetected` event will be generated when an external NFC field is detected. Optionally you may call `startEmulation()` to immediately start the card emulation regardless if an NFC reader is present. 2. Wait for `Event.readerDetected` to detect the presence of an external NFC reader. 3. Call `startEmulation()` when you app is ready to handle APDUs sent by the reader. This is when card emulation is enabled and reader can be connected to the device. 4. When `Event.received()` is received, process the APDU data, generate a response, and send it out using `APDU.respond(response:)`. If you don't send out the response, reader may detect a timeout conditions and generates error condition. 5.`Event.readerDeselected` is triggered when reader's field is turned off or device is pull away from the reader. 6. Call `invalidate()` to terminate the session. At any time `stopEmulation(status:)` can be called to stop card emulation (after emulation has been started previously). Note that stopping emulation at the wrong time can cause RF tearing and triggers error on the reader. NOTES: - Session is valid up to 60 seconds starting from the first`startEmulation()` call. Once this timeout occurs, `sessionInvalidated(reason: .maxSessionDurationReached)` will be returned from the event stream and the session will be invalidated. - A modal UI with text controlled by `alertMessage` will be shown on top of your app after `startEmulation()` is called. It will get dismissed when `stopEmulation(status:)` is called or session is invalidated. - `systemNotAvailable` indicates system is currently unavailable to accept your request; this is a temporal condition and you may retry the operation in a later time. - Special entitlement is required to use this service. As part of this entitlement you are required to declare a list of ISO7816 application identifiers (AID) prefixes that your application will be listen on when receiving the SELECT command from the reader. A `fatalError()` will occur on `CardSession.init()` if the necessary entitlement is missing. - `isEligible` can be used to determined if system is eligible to use the card session before instantiation. - `isSupported` shall be used to determine if the device supports NFC card session functionality. `fatalError()` will get triggered when `CardSession.init()` is called on unsupported hardware.

Members (10)

method · iOS 17.4+

invalidate

TypeScript
invalidate(): void
Swift declaration
func invalidate()

Invalidate session; emulation will be stopped immediately if it is still running. A new `CardSession` instance is required to restart.

get · iOS 17.4+

isEligible

TypeScript
static get isEligible(): Promise<boolean>
Swift declaration
static var isEligible: Bool { get async }

Query system eligibility for using card session. You may want to query this result before calling `CardSession.createSession()` to avoid any system UI overlay to be shown.

get · iOS 17.4+

eventStream

TypeScript
get eventStream(): CardSession_EventStream
Swift declaration
var eventStream: CardSession.EventStream { get }

Event stream for CardSession events.

get · iOS 17.4+

isSupported

TypeScript
static get isSupported(): boolean
Swift declaration
class var isSupported: Bool { get }

Return if current device supports card session functionality. CardSession APIs shall not be used if this returns false.

get · iOS 17.4+

alertMessage

TypeScript
get alertMessage(): string
Swift declaration
var alertMessage: String { get set }

Descriptive text message that is displayed on the alert action sheet once card emulation has started. The string can be updated dynamically in any thread context as long as the session is valid. This should be set prior to calling `startEmulation()` to display the correct message. Use this string to provide additional context about the NFC card emulation process.

set · iOS 17.4+

alertMessage

TypeScript
set alertMessage(value: string)
Swift declaration
var alertMessage: String { get set }

Descriptive text message that is displayed on the alert action sheet once card emulation has started. The string can be updated dynamically in any thread context as long as the session is valid. This should be set prior to calling `startEmulation()` to display the correct message. Use this string to provide additional context about the NFC card emulation process.

method · iOS 17.4+

stopEmulation

TypeScript
stopEmulation(status: CardSession_EmulationUIStatus): Promise<void>
Swift declaration
func stopEmulation(status: CardSession.EmulationUIStatus) async

Stop emulation.

method · iOS 17.4+

startEmulation

TypeScript
startEmulation(): Promise<void>
Swift declaration
func startEmulation() async throws

Attempt to start card emulation; will throw if unable to start. Upon success, a modal UI will be shown on top of your application.

get · iOS 17.4+

isEmulationInProgress

TypeScript
get isEmulationInProgress(): Promise<boolean>
Swift declaration
var isEmulationInProgress: Bool { get async }

Return status of card emulation.

init · iOS 17.4+

init

TypeScript
static init(): Promise<CardSession>
Swift declaration
init() async throws

Request to start a card session. Successful session start depends on the following conditions: - Device eligibility for using card session. - User has accepted your app request to use card session. Refer to `CardSession.Error` on possible errors. `fatalError()` will get triggered if `isSupported` returns false.

Skipped declarations

No declarations were skipped in this snapshot.

All CoreNFC types →