Skip to content

Add Eye Tracking to iOS

Overview

Obital SDK for iOS is distributed through CocoaPods. CocoaPods makes manages all dependencies for the framework, and allows you to easily update the SDK without breaking anything.

Install CocoaPods

Install CocoaPods using Terminal:

sudo gem install cocoapods

Configure CocoaPods

If you don't have one already, create a new XCode project and cd to it. Initialize CocoaPods for the project by running

pod init

This will generate a series of files in the directory related to CocoaPods. Locate and open Podfile in a text editor of your choice. Add the following dependencies to the file below the use_frameworks! line:

Note

You need access to our repository to use our framework. Contact sales to get started.

pod 'Obital', '0.1.9', :source => "https://github.com/Obital-io/Podspecs.git"
pod 'Obital_iOS', '1.17.4' , :source => "https://github.com/Obital-io/Podspecs.git"

Now install the frameworks by running pod install in Terminal. You should now be able open the YOUR_PROJECT_NAME.xcworkspace file in the directory, and build it.

Set Up Permissions

As the SDK uses the camera API, you need to ask for permission from the user. Locate info.plist in you XCode project and add a new entry with the key NSCameraUsageDescription. Add a value, describing what you need it for. For example To Track Eyes.

Import the SDK and Start Eye Tracking Session

Open your project in XCode and locate the main view controller. Import the SDK

import Obital_iOS

Create a class variable referencing the SessionManager in the view controller:

var sessionManager: SessionManager?

Now instantiate it in viewDidLoad():

override func viewDidLoad() {
    super.viewDidLoad()
    // Create the Session Manager
    sessionManager = SessionManager()
    // Start Eye Tracking
    sessionManager!.start()
}

The eye tracking session is now running!

Listen to Eye Tracking Updates

The Session Manager posts all eye tracking updates using the NotificationCenter in Swift. Register the observer in the viewDidLoad() method:

override func viewDidLoad() {
    // Previous code goes here
    NotificationCenter.default.addObserver(self,
                                           selector: #selector(didUpdateLookAtPoint),
                                           name: .didUpdateLookAtPoint,
                                           object: nil)
}

@objc func didUpdateLookAtPoint(notification: Notification) {
    guard let point = notification.object as? CGPoint else { return }
    print(point)
}

Since Obital_iOS only runs on iOS12+, you do not need to unregister the observer on deallocation, as per Apple's guidelines.

You can receive the following eye tracking information using the following Notification Names and corresponding object types:

Name Object Description
didUpdateCalibratedGazePoint CGPoint Notifies the observer of any updated gaze point. No notification is posted here, if the system is not calibrated.
"didUpdateLookAtPointPoint" CGPoint For any updated Look At Point. The lookAtPoint is an estimation of the optical axis in ARKit coordinate space, and doesn't require calibration.
didUpdateEyes - For updates on the eye rotations and positions. Doesn't require calibration.
didUpdateFaceDetectedStatus Bool Updates every time the face tracking status changes. For example, when moving the camera out of view of a face, it send a notification with a false object.

See Terminology for explanation of terms.

Run the project. When moving your face in front of the camera, you should now see the lookAtPoint positions printed in your console! The lookAtPoint in itself does not provide much value. Follow the next tutorial to Calibrate The Eye Tracker!