Requirements
- Xcode 16 or later
- An app that targets iOS 17 or later
- An iPhone to run it on. The Simulator has no camera.
Add a scan field
1
Add the package
In Xcode, choose File → Add Package Dependencies…, paste the package URL, and add ZipZapKit to your app target.If you manage dependencies in
Package.swift, add it there instead:2
Say why you need the camera
iOS won’t open the camera unless your app explains why. In your target’s Info tab, add Privacy - Camera Usage Description (
NSCameraUsageDescription) with a sentence your users will see, such as “Scan barcodes to look up products.”Without it, the app crashes the first time the camera opens.3
Add a ScanField
Use That’s all the code. The field shows a camera button, opens the camera when it’s tapped, and writes what it scans into
ScanField anywhere you’d use a TextField:code.4
Run it on your iPhone
Build and run on a device, tap the camera button in the field, and allow camera access when iOS asks.
Check it works
Point the camera at this barcode: The field should fill in with9521234567899, and “Scanned: 9521234567899” should appear below it.
If nothing happens:
- The camera button is missing. You’re running in the Simulator. Run on a device, or type a number and press Return to test the rest of your screen.
- The app crashes when the camera opens. The camera usage description is missing from the target that’s running.
- The camera opens but doesn’t scan. Move back until the whole barcode is in view, and raise your screen brightness.
Make it specific
A field that accepts any barcode will also accept the wrong one. Tell it what you expect:Next steps
Replace a TextField
Swap ScanField into a form you already have.
Validation
Reject scans that don’t match what you expect.
Scan sources
How the camera, HID scanners and ZipZap hardware feed the same field.
GS1 parsing
Pull the GTIN, expiry date and batch out of one barcode.
When the camera isn’t enough
MagSafe backpack scanner
A MagSafe scanner that attaches to the back of an iPhone.
Captive case scanner
An iPhone case with a built-in scan engine.