Skip to main content

IOS Integration

Please follow these steps for integrating OCR Lite xcframework.

note

You can find sample project containing complete integration of SDK here (requires access).

Integration Guide (Swift)

Pods Linking

For linking OcrLite library using CocoaPods you can add either:

  • OcrLite repository link with version tag in your Podfile
pod 'Ocr', :git => "https://gitlab.com/fetchsky/peekaboo-connect-studio/identity/ocr-lite-pod", :tag => "vX.X.X"

Use the latest communicated version of the framework.

  • Or place OcrLite directory in your project root and its path in your Podfile
pod 'Ocr', :path => "./OcrLite"

Run pod install in your terminal.

Using Library

Link Ocr in your ViewController

import Ocr

Conform to OcrControllerDelegate protocol in your class to handle events and results related to the framework.

class ViewController: OcrControllerDelegate { ....

Initialize OcrViewController

Create an instance of OcrViewController with the required parameters:

let ocrController = OcrViewController(
cnicNumber: "your_cnic_number",
skipProcessing: false,
blurThreshold: 100,
imageWidth: 1000
)

Parameters:

  • cnicNumber: String containing the CNIC number to validate against
  • skipProcessing: Boolean to skip processing (default: false)
  • blurThreshold: Float value to validate if captured frame is blurred or shaky (default: 100)
  • imageWidth: Integer specifying the width of captured images (default: 1000)

Setup and Display

// Set Delegate
ocrController.delegate = self

// Add as Child View Controller
addChild(ocrController)

// Configure View Frame
ocrController.view.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)

// Add as Subview
view.addSubview(ocrController.view)

// Notify Child View Controller
ocrController.didMove(toParent: self)

Event Handlers

Implement the required delegate methods:

class ViewController: OcrControllerDelegate {

func handleInfo(message: String) {
// Handle informational messages from the framework
print("Info: \(message)")
}

func handleError(code: String) {
// Handle errors from the framework
// Error codes can be: initialization_error, invalid_source, model_error, no_camera, etc.
print("Error: \(code)")

// Remove the OCR controller view when error occurs
ocrController?.view?.removeFromSuperview()
}

func handleResponse(identityNumber: String, frontImage: String, backImage: String, documentType: DocumentType, distance: Double) {
// Handle successful OCR response
// identityNumber: Extracted identity number from the document
// frontImage: Base64 encoded front image of the document
// backImage: Base64 encoded back image of the document
// documentType: Type of document detected
// distance: Distance measurement for validation

print("Identity Number: \(identityNumber)")
print("Document Type: \(documentType)")
print("Distance: \(distance)")

// Remove the OCR controller view after successful processing
ocrController?.view?.removeFromSuperview()
}
}

Complete Implementation Example

class ViewController: UIViewController, OcrControllerDelegate {

var ocrController: OcrViewController? = nil
var blurThreshold: Float = 100

@IBAction func launchOCR(_ sender: Any) {
// Create OCR controller instance
ocrController = OcrViewController(
cnicNumber: "your_cnic_number",
skipProcessing: false,
blurThreshold: blurThreshold,
imageWidth: 1000
)

if let ocrController = ocrController {
// Setup and display
ocrController.delegate = self
addChild(ocrController)
ocrController.view.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)
view.addSubview(ocrController.view)
ocrController.didMove(toParent: self)
}
}

// Implement delegate methods as shown above
}

Updating Info.plist

Add Privacy - Camera Usage Description in your app's Info.plist to allow access to camera.