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.
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.
| Operation | What happens | Callback |
|---|---|---|
| Detection | The camera opens, finds the document, checks capture quality and crops it | withDetectionResult |
| Scan | Once per document. The captured images are sent together to the server, which classifies the document, extracts the data and runs the validations | andScanResult |
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.
- Swift
- Objective-C
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()
}
}
}
- (void)detectDocumentSide:(MobbScanDocumentSide)side
onAccepted:(void (^)(UIImage *))onAccepted {
[self.mobbScanApi detectDocumentSide:side
withDetectionResult:^(MobbScanDetectionResult result,
MobbScanDetectionResultData *resultData,
NSError *error) {
if (result == MobbScanDetectionResult_ERROR || error != nil || resultData.image == nil) {
// Handle the detection failure or cancellation
return;
}
// App-defined preview: accept the image or retry this same side.
[self showPreview:resultData.image completion:^(BOOL accepted) {
if (accepted) {
onAccepted(resultData.image);
} else {
[self detectDocumentSide:side onAccepted:onAccepted];
}
}];
}];
}
- (void)captureBothSides {
[self detectDocumentSide:MobbScanDocumentSide_FRONT onAccepted:^(UIImage *frontImage) {
self.frontImage = frontImage;
[self detectDocumentSide:MobbScanDocumentSide_BACK onAccepted:^(UIImage *backImage) {
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
| Parameter | Description |
|---|---|
MobbScanDetectionResult | OK or ERROR |
MobbScanDetectionResultData | The cropped image (image) and the detected side |
NSError | Error 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.
- Swift
- Objective-C
private func scanDocument() {
mobbScanApi.scanDocument(withFrontSide: frontImage,
backSide: backImage,
forScanId: scanId) { result, data, error in
// Handle the document scan result
}
}
- (void)scanDocument {
[self.mobbScanApi scanDocumentWithFrontSide:self.frontImage
backSide:self.backImage
forScanId:self.scanId
andScanResult:^(MobbScanScanResult result,
MobbScanScanResultData *data,
NSError *error) {
// Handle the document scan result
}];
}
Scan result
MobbScanScanResult | Meaning | Next step |
|---|---|---|
COMPLETED | The document is complete | Continue with face matching |
ERROR | The document could not be processed | Check 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.
- Swift
- Objective-C
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()
}
}
}
}
- (void)scanBackOnlyDocument {
[self.mobbScanApi detectDocumentSide:MobbScanDocumentSide_BACK
withDetectionResult:^(MobbScanDetectionResult result,
MobbScanDetectionResultData *data,
NSError *error) {
if (result == MobbScanDetectionResult_ERROR || error != nil || data.image == nil) {
[self handle:error];
return;
}
[self showPreview:data.image completion:^(BOOL accepted) {
if (accepted) {
[self.mobbScanApi scanDocumentWithFrontSide:nil
backSide:data.image
forScanId:self.scanId
andScanResult:^(MobbScanScanResult result,
MobbScanScanResultData *scanData,
NSError *scanError) {
// 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:
- Swift
- Objective-C
MobbScanAPI.getInstance().configurePDF417DetectionStep(true) // enable, 15 s timeout
MobbScanAPI.getInstance().configurePDF417DetectionStep(true, andTimeoutInSeconds: 20) // custom timeout
[[MobbScanAPI getInstance] configurePDF417DetectionStep:YES]; // enable, 15 s timeout
[[MobbScanAPI getInstance] configurePDF417DetectionStep:YES andTimeoutInSeconds:20]; // custom timeout
If the barcode cannot be read within the timeout, the flow continues with the regular document detection.
Related
- Texts — change the messages shown during detection.