Skip to main content

๐ŸŽ 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 and NSPhotoLibraryUsageDescription keys in Info.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