Skip to main content

Document scan

The document scan captures the required sides of the document with automatic detection, then sends them together to the MobbScan server, which extracts the data and returns the validation results.

Before you begin

Initialise the SDK and start a process to get a scanId.

How it works​

For a two-sided document, detect each side (retry if rejected), then scan the accepted images together once per document.

OperationWhat happensCallback
DetectionThe camera opens, finds the document, checks capture quality and crops itwithDetectionResult
ScanOnce per document. The captured images are sent together to the server, which classifies the document, extracts the data and runs the validationsandScanResult

Detect and review both sides before scanning. For a one-sided document or passport, use the side-specific scan.

1. Detect each side​

Call detect once for each side the selected operation mode requires. Show each returned image in your preview and call detect again for that side if the user rejects it. Do not scan until every required image has been accepted.

private func detectDocumentSide(
_ side: MobbScanDocumentSide,
onAccepted: @escaping (UIImage) -> Void
) {
mobbScanApi.detect(side) { result, data, error in
guard result != .ERROR, error == nil, let image = data?.image else {
// Handle the detection failure or cancellation
return
}

// App-defined preview: accept the image or retry this same side.
self.showPreview(image) { accepted in
if accepted {
onAccepted(image)
} else {
self.detectDocumentSide(side, onAccepted: onAccepted)
}
}
}
}

private func captureBothSides() {
detectDocumentSide(.FRONT) { frontImage in
self.frontImage = frontImage
self.detectDocumentSide(.BACK) { backImage in
self.backImage = backImage
self.scanDocument()
}
}
}

The SDK presents its own capture screen and dismisses it when the detection ends. Implement showPreview in your app: accepting an image continues to the next side, while rejecting it repeats detection for that side. Call scanDocument only after every required image has been accepted.

Detection result​

ParameterDescription
MobbScanDetectionResultOK or ERROR
MobbScanDetectionResultDataThe cropped image (image) and the detected side
NSErrorError code and details when the result is ERROR. See Error codes

2. Scan the captured images​

Once every required side is captured, submit the images together in a single scanDocument call.

private func scanDocument() {
mobbScanApi.scanDocument(withFrontSide: frontImage,
backSide: backImage,
forScanId: scanId) { result, data, error in
// Handle the document scan result
}
}

Scan result​

MobbScanScanResultMeaningNext step
COMPLETEDThe document is completeContinue with face matching
ERRORThe document could not be processedCheck the NSError

With SCAN_ONLY_BACK or SCAN_ONLY_FRONT, scanning the required side returns COMPLETED.

MobbScanScanResultData contains the document with the extracted fields (name, surname, document number, dates, nationality, MRZ…) and the result of each validation. The full list of fields and validations is described in the document object.

One-sided documents and passports​

For a passport (TD3) or another document that requires only the back, start the process with SCAN_ONLY_BACK and call scanBackOnlyDocument(). The example previews the detected image before scanning; rejecting it repeats detection. For a front-only document, use SCAN_ONLY_FRONT and apply the same sequence to .FRONT, passing nil for the back image.

private func scanBackOnlyDocument() {
mobbScanApi.detect(.BACK) { result, data, error in
guard result != .ERROR, error == nil, let backImage = data?.image else {
return self.handle(error)
}

self.showPreview(backImage) { accepted in
if accepted {
self.mobbScanApi.scanDocument(withFrontSide: nil,
backSide: backImage,
forScanId: scanId) { result, scanData, error in
// Process the one required side (for example, a passport)
}
} else {
self.scanBackOnlyDocument()
}
}
}
}

Documents with a PDF417 barcode​

Some documents, such as Colombian or Peruvian ID cards, carry a PDF417 barcode with additional data. Add a barcode reading step before the document detection to read it with higher accuracy:

MobbScanAPI.getInstance().configurePDF417DetectionStep(true) // enable, 15 s timeout
MobbScanAPI.getInstance().configurePDF417DetectionStep(true, andTimeoutInSeconds: 20) // custom timeout

If the barcode cannot be read within the timeout, the flow continues with the regular document detection.

  • Texts — change the messages shown during detection.