Search code examples
iosobjective-cswiftaffdex-sdk

How to use Affdex SDK in Swift?


We are trying to use the iOS Affdex SDK with a bridging header (in Swift). Can you please help us how to go about this process. Also how can we display up emojis based on the SDK (agin using Swift).


Solution

  • Here are some links to help you with Objective-C to Swift naming conventions:

    https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html

    I’ve attached a simple view controller class which shows how to use our SDK in Swift. Hopefully this will help you.

        class ViewController: UIViewController, AFDXDetectorDelegate {
    
        var detector : AFDXDetector? = nil
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // create the detector
            detector = AFDXDetector(delegate:self, usingCamera:AFDX_CAMERA_FRONT, maximumFaces:1)
            detector?.setDetectEmojis(true)
            detector!.start()
        }
    
        func detectorDidStartDetectingFace(face : AFDXFace) {
            // handle new face
        }
    
        func detectorDidStopDetectingFace(face : AFDXFace) {
            // handle loss of existing face
        }
    
        func detector(detector : AFDXDetector, hasResults : NSMutableDictionary?, forImage : UIImage, atTime : NSTimeInterval) {
            // handle processed and unprocessed images here
            if hasResults != nil {
                // handle processed image in this block of code
    
                // enumrate the dictionary of faces
                for (_, face) in hasResults! {
                    // for each face, get the rage score and print it
                    let emoji : AFDXEmoji = face.emojis
                    let rageScore = emoji.rage
                    print(rageScore)
                }                
            } else {
                // handle unprocessed image in this block of code
            }
        }