๐ iOS Quickstart Guide
This guide helps you integrate the Visionary.ai API into your iOS app using Swift.
Youโll be able to send images from the device to the API and receive real-time analysis results.
๐ฆ Prerequisitesโ
- Xcode 13 or higher
- iOS 14+
NSCameraUsageDescription
andNSPhotoLibraryUsageDescription
keys inInfo.plist
- Internet access
๐งฑ Step 1 โ Install Dependenciesโ
You can use Alamofire or native URLSession
.
Weโll use URLSession
in this example for zero dependency.
No external libraries required โ
๐ผ๏ธ Step 2 โ Prepare the Image Dataโ
Convert your selected image into Data
format:
guard let image = UIImage(named: "sample.jpg"),
let imageData = image.jpegData(compressionQuality: 0.8) else {
return
}
Youโll need this to attach the image to the HTTP request.
๐ Step 3 โ Create the HTTP Requestโ
let url = URL(string: "https://api.visionary.ai/v1/analyze")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("Bearer YOUR_API_KEY", forHTTPHeaderField: "Authorization")
let boundary = UUID().uuidString
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
๐ค Step 4 โ Add the Image as Multipart/Form-Dataโ
var body = Data()
body.append("--\(boundary)\r\n".data(using: .utf8)!)
body.append("Content-Disposition: form-data; name=\"image\"; filename=\"photo.jpg\"\r\n".data(using: .utf8)!)
body.append("Content-Type: image/jpeg\r\n\r\n".data(using: .utf8)!)
body.append(imageData)
body.append("\r\n".data(using: .utf8)!)
body.append("--\(boundary)--\r\n".data(using: .utf8)!)
request.httpBody = body
๐ก Multipart form-data is the standard way to upload files via HTTP. You must build the body manually with line breaks and boundaries, otherwise the API will reject the request.
๐ Step 5 โ Send the Request and Handle the Responseโ
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print("โ Network error: \(error?.localizedDescription ?? "Unknown error")")
return
}
if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 {
do {
let result = try JSONDecoder().decode(AnalysisResponse.self, from: data)
print("โ
Success: \(result)")
} catch {
print("โ JSON decoding failed: \(error)")
}
} else {
print("โ API error: \(response.debugDescription)")
}
}
task.resume()
๐ก AnalysisResponse
should be a Swift struct matching the JSON response. You can use quicktype.io to generate it.
๐งช Testingโ
Use the demo-api-key
to test up to 20 requests per day.
Replace it with your real key for production use.
โ ๏ธ Notesโ
-
Max file size: 5MB
-
Supported formats:
.jpg
,.jpeg
,.png
,.webp
-
Avoid large screenshots or low-res images
-
Image must be attached under the field name
"image"
Need help? โ Check the Integration Troubleshooting guide โ Or read the FAQ