Smarty iOS SDK
An iOS client for Smarty’s address verification, geocoding, autocomplete, and data enrichment APIs.
The Smarty iOS SDK lets you integrate address intelligence into your iOS applications with minimal boilerplate. Whether you’re validating millions of addresses, enriching them with precise geocodes, or powering an autocomplete form, the SDK gives you a clear, iOS-native way to call every Smarty API.
Contents
- Installation
- Authentication
- Create an API client
- Create and send lookups
- Examine results
- More examples
Installation
You can import the Smarty iOS SDK straight from Xcode with minimal effort. Just select “File” > “Add Package Dependency,” and enter this URL in the search bar:
https://github.com/smartystreets/smartystreets-ios-sdk
Voila! The SDK is now available in your Xcode project.
If you want to import the SDK using Swift Package Manager, you can do so with a Package.swift like this:
// swift-tools-version: 6.0
import PackageDescription
let package = Package(
name: "MyPackage",
dependencies: [
.package(
url: "https://github.com/smartystreets/smartystreets-ios-sdk",
from: "9.0.0"
),
],
targets: [
.executableTarget(
name: "MyPackage",
dependencies: [
.product(name: "SmartyStreets", package: "smartystreets-ios-sdk"),
],
path: "Sources"),
]
)
Authentication
Smarty offers two kinds of credentials depending on where your code runs:
-
Server-side (backend, CLI, batch jobs)
Use Auth ID + Auth Token (a Secret Key pair). -
Client-side (browser, front-end, public UI)
Use Auth Web + Auth Referer (a Website Key pair restricted by domain).
You can find all keys in your Smarty account under:
Account → API Keys.
We recommend storing your credentials in environment variables for security:
export SMARTY_AUTH_ID="your-auth-id"
export SMARTY_AUTH_TOKEN="your-auth-token"
export SMARTY_AUTH_WEB="your-auth-web"
export SMARTY_AUTH_REFERER="your-auth-referer"
For details on authentication and embedded vs. secret keys, see our authentication documentation.
Create an API client
The SDK offers different client builders for each API.
Server-side client
Here’s an example for the US street address API:
let authId = ProcessInfo.processInfo.environment["SMARTY_AUTH_ID"] ?? ""
let authToken = ProcessInfo.processInfo.environment["SMARTY_AUTH_TOKEN"] ?? ""
let client = ClientBuilder.withBasicAuth(authId: authId, authToken: authToken).buildUsStreetApiClient()
Browser/Frontend client
Here’s an example for the international autocomplete API:
let id = getEnvironmentVar("SMARTY_AUTH_WEB") ?? ""
let hostname = getEnvironmentVar("SMARTY_AUTH_REFERER") ?? ""
let client = ClientBuilder(id: id, hostname: hostname).buildInternationalAutocompleteApiClient()
You can find a full list of API clients here.
Create and send a lookup
Create lookups based on the addresses you want to validate. Here’s an example for the US street address API:
var lookup = USStreetLookup()
lookup.street = "1600 Amphitheatre Pkwy"
lookup.secondary = "APT 2"
lookup.city = "Mountain View"
lookup.state = "CA"
lookup.zipCode = "94043"
Use the client you created earlier to send the lookup:
var error: NSError! = nil
_ = client.sendLookup(lookup: &lookup, error: &error)
if let error = error {
let output = """
Domain: \(error.domain)
Error Code: \(error.code)
Description: \(error.userInfo[NSLocalizedDescriptionKey] as! NSString)
"""
return output
}
Examine results
let results:[USStreetCandidate] = lookup.result
var output = "Results:\n"
if results.count == 0 {
return "Error. Address is not valid"
}
let candidate = results[0]
output.append("""
\nZIP Code: \(candidate.components?.zipCode ?? "")
\nCounty: \(candidate.metadata?.countyName ?? "")
\nLatitude: \(candidate.metadata?.latitude ?? 0.0)
\nLongitude: \(candidate.metadata?.longitude ?? 0.0)
"""
)
return output
More examples
- US street address API
- US autocomplete pro API
- US enrichment API
- US extract API
- US reverse geocoding API
- US ZIP Code API
- International autocomplete API
- International street address API
All iOS SDK examples can be found on our GitHub here.