CodeService

IAPMiniProgram SDK calls the CodeService SPI to launch the QR code or barcode scanner. For details on how the SPI functions, refer to the CodeService workflow.

This SPI provides the scan method. The method details are as follows:

scan

IAPMiniprogram SDK calls the scan method to launch the QR code or barcode scanner.

Method signature

copy
func scan(
    with option: IAPWalletScannerOption,
    in context: IAPWalletAPIContext? = nil,
    callback: @escaping (IAPWalletScannerResult) -> Void
)

Request parameters

Field

Data type

Description

Required

option

IAPWalletScannerOption

An object that specifies the details of the scan request from the mini program. When this parameter is not specified, the default values of this parameter are used.

Yes

context

IAPWalletAPIContext

A context object, which carries the mini program runtime metadata.

Yes

callback

Callback

The callback that is executed when request processing is completed. For details, see the following section.

Yes

Callback

Field

Data type

Description

Required

result

IAPWalletScannerResult

The result of the scan request.

Yes

Response parameters

N/A

Error code

Error code

Error message

10

ERROR_USER_CANCEL

Users cancel the scan action.

2001

ERROR_OPERATION_FAILED

Failed to scan the code.

Sample

The following sample shows how to implement the CodeService SPI to enable QR code and barcode scanning capabilities.

  1. Create a class that implements the CodeService interface.
  2. Implement the scan method.

Swift

copy
final class CodeService: IAPWalletCodeServiceSignature {
    override func scan(
        with option: IAPWalletScannerOption,
        in context: IAPWalletAPIContext? = nil,
        callback: @escaping (IAPWalletScannerResult) -> Void
    ) {
        // to scan code
        let scanResult = IAPWalletScannerResult(code: "CODE_VALUE")
        callback(scanResult)
    }
}