Normalize a Street Address in Swift
--
When retrieving user information, such as addresses, from text strings in electronic correspondence, incompatible formatting is a common occurrence. To avoid this potential issue, you can use the following API to automatically normalize and validate an input street address; it supports all major international addresses, ensuring full coverage of your customer base.
All you will need for this operation is your parse request (full address) and API key, which you can input into the below example code:
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endifvar semaphore = DispatchSemaphore (value: 0)let parameters = "{\n \"StreetAddress\": \"<string>\",\n \"City\": \"<string>\",\n \"StateOrProvince\": \"<string>\",\n \"PostalCode\": \"<string>\",\n \"CountryFullName\": \"<string>\",\n \"CountryCode\": \"<string>\"\n}"
let postData = parameters.data(using: .utf8)var request = URLRequest(url: URL(string: "https://api.cloudmersive.com/validate/address/street-address/normalize")!,timeoutInterval: Double.infinity)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("YOUR-API-KEY-HERE", forHTTPHeaderField: "Apikey")request.httpMethod = "POST"
request.httpBody = postDatalet task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
semaphore.signal()
return
}
print(String(data: data, encoding: .utf8)!)
semaphore.signal()
}task.resume()
semaphore.wait()
The returned response will indicate if the address is valid, as well as the latitude and longitude if it is. To retrieve your API key, visit the Cloudmersive website and register for a free account; this provides 800 monthly calls across any of our APIs.