Skip to content

Implementation

Import

Import
In your sources, import IDLiveFaceCamera.

import IDLiveFaceCamera

License

Capture library requires a valid license to access its features. Use the License.setLicense method to set your license key. This method requires two parameters:

  • licenseKey: a string representing your unique license key.
  • licenseType: an enumeration value representing the type of license. For example, .faceDetector for using the face detection features.

Below is a sample code snippet demonstrating how to set the license:

do {
  try License.setLicense("your license key", licenseType: .faceDetector)
} catch {
  print("Set license error: \(error)")
}

Instantiate

Instantiate and configure the capture module
Instantiate an IDCameraController. It will communicate with your code by using IDCameraControllerDelegate protocol. Set your class as delegate.
Configure it's behavior as necessary (see the framework docs for available options). Setup face detector settings if needed (including face detection engines to use).

If you plan to use IAD, call prepareForIAD() method on IDCameraController. This will prepare the camera controller for collecting the necessary data.

// instantiate the camera controller from storyboard
guard let vc = IDCameraController.instantiate() else {
    print("can't instantiate camera viewcontroller")
    return
}

// set options before showing
vc.delegate = self
vc.allowToPreviewBeforeSending = true

// prepare for IAD
vc.prepareForIAD()

Present the controller

Present
Present the controller.
It will ask the user for photo permission if needed, but you might want to check that in advance before presenting it.

self.show(vc, sender: nil)

User will take a photo, either manually or automatically using face detection. When it happens, delegate method cameraController(_, didCaptureImage:) will be called.

Receive the photo

Receive the photo
Receive the photo from the delegate method parameter. Alternatively, if you wish to use IAD, use createIADBundle() method to create the IAD bundle. This binary bundle will contain all necessary information for sending to server, including the photo.

func cameraController(_ controller: IDCameraControllerBase, didCaptureImage image: IDImage) {
    controller.currentMode = .busy // set visual status to "busy" while async operation is running
    print("user took a photo")

    let bundle = try? createIADBundle() // process the exception if needed

    // process the image/bundle, e.g. show on screen/send to server
    // ...
}

Process the photo

Process the photo
Process photo/IAD bundle as necessary.
If you are using your own server, send the data to your endpoint and receive the result.

Display the result

Display the result
If desired, use IDCameraController.setCurrentMode(_, message:) method to show the result within the controller. Choose the desired ViewMode based on whether the result result of your check was successfull: ViewMode.resultSuccess/.resultFailure/.result show the green (checkmark), red (cross) or black result button accordingly.

cameraController.setCurrentMode(success ? .resultSuccess : .resultFailure,
                                message: message)

Dismiss the controller

Dismiss the controller
When user clicks the result button, delegate method cameraControllerUserDidPressResultButton(_) will be called, at that time you can dismiss the camera controller.

func cameraControllerUserDidPressResultButton(_ controller: IDCameraControllerBase) {
    print("user has finished")
    controller.dismiss(animated: true)
}

User also might dismiss the controller manually. You can track this by using the UIAdaptivePresentationControllerDelegate protocol.

If delegate method cameraController(_, didEncounterError error:) is called, dismiss the camera controller and handle the error in your app.

func cameraController(_ controller: IDCameraControllerBase, didEncounterError error: IDCameraController.Error) {
    print("camera error: \(error), dismissing")
    controller.dismiss(animated: true)
}

You might also want to dismiss the camera controller in case the app resigns active (UIApplication.willResignActiveNotification).

Face Detector Result Type

Case Description
ok The photograph is fine and face has passed all checks, but delay hasn't passed
fire The photo is fine and face has passed all checks also delay has passed, take photo
tooManyFaces There is more than one face in the picture.
faceNotFound No face is found in the camera-taken image.
faceTooSmall The face is too far away from the camera or occupies a small area of the image.
faceTooBig The face is too big (too close to the camera)
faceCropped The face is only partially visible.
faceOffCenter The face is not in the center.
faceAngled The face is too big of an angle relative to the camera's view angle.
movementDetected The face is moved.
photoLowQuality The photo quality is not good enough.
photoBlurred A blur was detected.
photoBadLighting The frame lighting is bad.
customUserError A custom face status that you can set to show to the user.
faceDetectionError Face detection error, face detection cycle.
faceDetectionIgnorableError Face detection error that shouldn't stop face detection process.
faceDetectionFatalError Fatal face detection error, face detection should be stopped
unknownError We can't make any statements about the face because we don't have enough information.

Some messages with details in english are also possible to get from:

public enum FaceDetectorResult: Equatable {

 case ok, fire, ...
 ...
 public var message: String { ... } // returns message with details for specified case

 public var isError: Bool { ... } // returns false if all face validation are passed.
}