This library provides useful functionality for food and nutrition apps in SwiftUI, including:
To lookup a food item from the OpenFoodFacts database, you can use the OpenFoodFactsAPI
class.
import NutritionKit
let api = OpenFoodFactsAPI.shared
let foodItem = try await api.find("59032823")
print(foodItem.productName) // Prints "Nutella - 630g"
To configure the fields you are interested in, you can use the configure
function:
api.configure(productFields: [.productName, .servingSize])
NutritionKit provides a SwiftUI view for scanning generic barcodes, BarcodeScannerView
. To use this view, you must set the value Privacy - Camera Usage Description
in your app's Info.plist
.
By itself BarcodeScannerView
only shows the live camera feed with no overlay or other info. You can provide more information yourself by embedding it in a ZStack
or in another view.
import NutritionKit
struct ContentView: View {
@State var barcodeData: Barcode? = nil
var body: some View {
BarcodeScannerView(barcodeData: $barcodeData)
.onChange(of: barcodeData) { data in
// A barcode was detected in the camera feed
}
}
}
NutritionKit can detect and parse nutrition labels. Currently, only English and German labels are supported. Nutrition labels are scanned similarly to barcodes with the NutritionLabelScannerView
.
import NutritionKit
struct ContentView: View {
@State var nutritionLabel: NutritionLabel? = nil
var body: some View {
NutritionLabelScannerView(nutritionLabel: $nutritionLabel)
.onChange(of: nutritionLabel) { data in
// A nutrition label was detected in the camera feed
}
}
}
Currently, the following information can be detected:
link |
Stars: 3 |
Last commit: 1 week ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics