Sample code for 30+ languages & platforms
Swift Requires Chilkat v11.0.0+

Look up an IP location with IPinfo Core

See more Geolocation Examples

Use an IPinfo token with Core, Plus, or Max access. The current api.ipinfo.io/lookup/ endpoint returns location fields inside geo and network fields inside as. Authenticate with a Bearer token.

IPinfo continues to support its older ipinfo.io API for existing integrations. This example uses the current Core response structure. The free Lite API uses /lite/ and a different, country-level response. Choose the endpoint and parser for your subscription.

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.

Service documentation

Chilkat Swift Downloads

Swift

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"
    http.authToken = "IPINFO_TOKEN"
    let url = CkoStringBuilder()!
    url.append(value: "https://api.ipinfo.io/lookup/")
    url.append(value: http.urlEncode(str: ipAddress))
    let response = CkoHttpResponse()!
    success = http.httpNoBody(verb: "GET", url: url.getAsString(), 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: "error.title").intValue == 1 {
                print("\(json.string(of: "error.title")!)")
            }

            if json.jsonType(of: "error.message").intValue == 1 {
                print("\(json.string(of: "error.message")!)")
            }

        }

        return
    }

    if parsed != true {
        print("Expected a JSON object in the lookup response.")
        return
    }

    if (json.jsonType(of: "error").intValue == 3) {
        print("The service could not complete the lookup.")
        if json.jsonType(of: "error.title").intValue == 1 {
            print("\(json.string(of: "error.title")!)")
        }

        if json.jsonType(of: "error.message").intValue == 1 {
            print("\(json.string(of: "error.message")!)")
        }

        return
    }

    if (json.jsonType(of: "bogon").intValue == 5) && (json.bool(of: "bogon") == true) {
        print("This is a reserved or otherwise non-public IP address.")
        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: "geo.country").intValue == 1) && (json.string(ofEquals: "geo.country", value: "", caseSensitive: true) != true) {
        print("Country: \(json.string(of: "geo.country")!)")
    }
    else {
        print("Country: unavailable")
    }

    if (json.jsonType(of: "geo.country_code").intValue == 1) && (json.string(ofEquals: "geo.country_code", value: "", caseSensitive: true) != true) {
        print("Country code: \(json.string(of: "geo.country_code")!)")
    }
    else {
        print("Country code: unavailable")
    }

    if (json.jsonType(of: "geo.region").intValue == 1) && (json.string(ofEquals: "geo.region", value: "", caseSensitive: true) != true) {
        print("Region: \(json.string(of: "geo.region")!)")
    }
    else {
        print("Region: unavailable")
    }

    if (json.jsonType(of: "geo.city").intValue == 1) && (json.string(ofEquals: "geo.city", value: "", caseSensitive: true) != true) {
        print("City: \(json.string(of: "geo.city")!)")
    }
    else {
        print("City: unavailable")
    }

    if (json.jsonType(of: "geo.timezone").intValue == 1) && (json.string(ofEquals: "geo.timezone", value: "", caseSensitive: true) != true) {
        print("Time zone: \(json.string(of: "geo.timezone")!)")
    }
    else {
        print("Time zone: unavailable")
    }

    //  Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
    if (json.jsonType(of: "geo.latitude").intValue == 2) && (json.jsonType(of: "geo.longitude").intValue == 2) {
        print("Coordinates: \(json.string(of: "geo.latitude")!), \(json.string(of: "geo.longitude")!)")
    }
    else {
        print("Coordinates: unavailable")
    }

    if (json.jsonType(of: "as.asn").intValue == 1) && (json.string(ofEquals: "as.asn", value: "", caseSensitive: true) != true) {
        print("ASN: \(json.string(of: "as.asn")!)")
    }
    else {
        print("ASN: unavailable")
    }

    if (json.jsonType(of: "as.name").intValue == 1) && (json.string(ofEquals: "as.name", value: "", caseSensitive: true) != true) {
        print("Network organization: \(json.string(of: "as.name")!)")
    }
    else {
        print("Network organization: unavailable")
    }


}