Swift Requires Chilkat v11.0.0+
Swift
Look up an IP address with ipdata
See more Geolocation Examples
Use an ipdata API key to request a public IPv4 or IPv6 address. The key is encoded as the api-key query parameter. The example displays location fields and the optional ASN object using its current nested structure. Data access and quotas depend on the account plan.
IP geolocation estimates a network location. VPNs, mobile gateways, and anycast routing can affect the result. City and coordinate fields can be missing or approximate. The sample address 8.8.8.8 is a public DNS address used to demonstrate the request.
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Use a public IP address. Provider results can change as their databases are updated.
var ipAddress: String? = "8.8.8.8"
let http = CkoHttp()!
http.requireSslCertVerify = true
http.connectTimeout = 10
http.readTimeout = 30
http.accept = "application/json"
var apiKey: String? = "YOUR_API_KEY"
let url = CkoStringBuilder()!
url.append(value: "https://api.ipdata.co/")
url.append(value: http.urlEncode(str: ipAddress))
let parameters = CkoJsonObject()!
parameters.updateString(jsonPath: "api-key", value: apiKey)
let response = CkoHttpResponse()!
success = http.httpParams(verb: "GET", url: url.getAsString(), json: parameters, response: response)
if response.statusCode.intValue == 0 {
// Request diagnostics can contain an API key. Report transport failure without logging credentials.
print("No HTTP response. Check connectivity, DNS, and TLS configuration.")
return
}
let json = CkoJsonObject()!
var parsed: Bool = json.load(json: response.bodyStr)
if (success != true) || (response.statusCode.intValue < 200) || (response.statusCode.intValue >= 300) {
print("Lookup failed. HTTP \(response.statusCode.intValue)")
if parsed == true {
if json.jsonType(of: "message").intValue == 1 {
print("\(json.string(of: "message")!)")
}
}
return
}
if parsed != true {
print("Expected a JSON object in the lookup response.")
return
}
if (json.jsonType(of: "ip").intValue != 1) || (json.string(ofEquals: "ip", value: "", caseSensitive: true) == true) {
print("The response did not contain the expected IP address.")
return
}
print("IP address: \(json.string(of: "ip")!)")
if (json.jsonType(of: "country_name").intValue == 1) && (json.string(ofEquals: "country_name", value: "", caseSensitive: true) != true) {
print("Country: \(json.string(of: "country_name")!)")
}
else {
print("Country: unavailable")
}
if (json.jsonType(of: "country_code").intValue == 1) && (json.string(ofEquals: "country_code", value: "", caseSensitive: true) != true) {
print("Country code: \(json.string(of: "country_code")!)")
}
else {
print("Country code: unavailable")
}
if (json.jsonType(of: "region").intValue == 1) && (json.string(ofEquals: "region", value: "", caseSensitive: true) != true) {
print("Region: \(json.string(of: "region")!)")
}
else {
print("Region: unavailable")
}
if (json.jsonType(of: "city").intValue == 1) && (json.string(ofEquals: "city", value: "", caseSensitive: true) != true) {
print("City: \(json.string(of: "city")!)")
}
else {
print("City: unavailable")
}
if (json.jsonType(of: "time_zone.name").intValue == 1) && (json.string(ofEquals: "time_zone.name", value: "", caseSensitive: true) != true) {
print("Time zone: \(json.string(of: "time_zone.name")!)")
}
else {
print("Time zone: unavailable")
}
// Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if (json.jsonType(of: "latitude").intValue == 2) && (json.jsonType(of: "longitude").intValue == 2) {
print("Coordinates: \(json.string(of: "latitude")!), \(json.string(of: "longitude")!)")
}
else {
print("Coordinates: unavailable")
}
if (json.jsonType(of: "asn.asn").intValue == 1) && (json.string(ofEquals: "asn.asn", value: "", caseSensitive: true) != true) {
print("ASN: \(json.string(of: "asn.asn")!)")
}
else {
print("ASN: unavailable")
}
if (json.jsonType(of: "asn.name").intValue == 1) && (json.string(ofEquals: "asn.name", value: "", caseSensitive: true) != true) {
print("Network organization: \(json.string(of: "asn.name")!)")
}
else {
print("Network organization: unavailable")
}
}