Configure the SDK (UIKit)

  • To configure the SDK using SDK key, we need to provide a SDK license key. Obtain your Passio key from the Passio developer portal.

  • To configure the SDK using Proxy URL, the SDK key is not required. The SDK will be on remoteOnly mode in that case. You need to provide following things when configuring the SDK using Proxy URL:

    • Proxy URL - Base url of the target proxy endpoint

    • Proxy headers - Required headers to all of the requests

  • Create a new or open your existing project in Xcode and import the framework inside the View Controller

import PassioNutritionAISDK
  • Define reference of SDK inside your View Controller

private let passioSDK = PassioNutritionAI.shared
  • Configure the SDK using PassioConfiguration shown in below code. Call configurePassioSDK() when you need to configure SDK e.g. inside viewDidLoad() function.

// Using SDK key

func configurePassioSDK() {
    
    passioSDK.statusDelegate = self
    
    let PASSIO_KEY = "YOUR_PASSIO_KEY"
    var passioConfig = PassioConfiguration(key: PASSIO_KEY)
    passioConfig.remoteOnly = true

    passioSDK.configure(passioConfiguration: passioConfig) { status in
        print("Mode = \(status.mode)")
        print("Missing files = \(String(describing: status.missingFiles))")
    }
}
// Using Proxy URL

func configurePassioSDK() {
    
    passioSDK.statusDelegate = self
    
    var passioConfig = PassioConfiguration()
    // Set the base URL of the target proxy endpoint
    passioConfig.proxyUrl = "https://yourdomain.com/nutrition/"
    // Set headers as per your requirements
    passioConfig.proxyHeaders = ["header" : "value"] 

    passioSDK.configure(passioConfiguration: passioConfig) { status in
        print("Mode = \(status.mode)")
        print("Missing files = \(String(describing: status.missingFiles))")
    }
}

The models required for the SDK to run local recognition can take some time for the first download. If the local recognition is not needed, setting the remoteOnly to true on the PassioConfiguration process will skip the download of the models. Remote recognition functions like recognizeImageRemote or searchForFood will still work.

When the SDK is configured without SDK key (using Proxy URL), the SDK will be remoteOnly.

  • Implement the PassioStatusDelegate protocol to receive configuration status updates. While the files are being downloaded, we can show in UI how many files are remanning for the download.

extension ImageSelectionVC: PassioStatusDelegate {
    
    func passioStatusChanged(status: PassioStatus) {
        print("Status changed: \(status)")
        configureUI(status: status)
    }
    
    func passioProcessing(filesLeft: Int) {
        print("Files to download: \(filesLeft)")
    }
    
    func completedDownloadingAllFiles(filesLocalURLs: [FileLocalURL]) {
        print("All files downloaded")
    }
    
    func completedDownloadingFile(fileLocalURL: FileLocalURL, filesLeft: Int) {
        print("File downloaded: \(fileLocalURL)")
    }
    
    func downloadingError(message: String) {
        print("Downloading error: \(message)")
    }
}
  • Update the UI based on the configuration status. The isReadyForDetection status indicated that the SDK is setup successfully and it is ready to use. The implementation of self.askForCapturePermission() method will be covered in the next section

func configureUI(status: PassioStatus) {
    
    DispatchQueue.main.async {
    
        switch status.mode {
        
        case .isReadyForDetection:
            // The SDK is ready for detection. Hide the status view.
            self.statusView.isHidden = true  
            // This will be covered in the next section
            self.askForCapturePermission()
            
        case .isBeingConfigured:
            self.statusLabel.text = "Configuraing SDK..."
            
        case .failedToConfigure:
            self.statusLabel.text = "SDK failed to configure!"
            
        case .isDownloadingModels:
            self.statusLabel.text = "SDK is downloading files..."
            
        case .notReady:
            self.statusLabel.text = "SDK is not ready!"
            
        @unknown default:
            self.statusLabel.text = "Unknown passio status!"
        }
    }
}

Last updated