Sample code for 30+ languages & platforms
Swift

ipstack.com IPv4 Geolocation Lookup

See more Geolocation Examples

Demonstrates how to lookup Geolocation data for an IPv4 address using the ipstack.com REST API.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let http = CkoHttp()!

    // Lookup an IPv4 address: 149.250.207.170  (this was a randomly chosen address)
    var jsonStr: String? = http.quickGetStr(url: "http://api.ipstack.com/149.250.207.170?access_key=YOUR_ACCESS_KEY")
    if http.lastMethodSuccess == false {
        print("\(http.lastErrorText!)")
        return
    }

    let json = CkoJsonObject()!
    json.emitCompact = false
    success = json.load(json: jsonStr)

    print("\(json.emit()!)")

    // Sample output:
    // Use this online tool to generate parsing code from sample JSON: 
    // Generate Parsing Code from JSON

    // {
    //   "ip": "149.250.207.170",
    //   "type": "ipv4",
    //   "continent_code": "EU",
    //   "continent_name": "Europe",
    //   "country_code": "DE",
    //   "country_name": "Germany",
    //   "region_code": null,
    //   "region_name": null,
    //   "city": null,
    //   "zip": null,
    //   "latitude": 51.2993,
    //   "longitude": 9.491,
    //   "location": {
    //     "geoname_id": null,
    //     "capital": "Berlin",
    //     "languages": [
    //       {
    //         "code": "de",
    //         "name": "German",
    //         "native": "Deutsch"
    //       }
    //     ],
    //     "country_flag": "http:\/\/assets.ipstack.com\/flags\/de.svg",
    //     "country_flag_emoji": "\ud83c\udde9\ud83c\uddea",
    //     "country_flag_emoji_unicode": "U+1F1E9 U+1F1EA",
    //     "calling_code": "49",
    //     "is_eu": true
    //   }
    // }

    var ip: String?
    var v_type: String?
    var continent_code: String?
    var continent_name: String?
    var country_code: String?
    var country_name: String?
    var region_code: String?
    var region_name: String?
    var city: String?
    var zip: String?
    var latitude: String?
    var longitude: String?
    var locationGeoname_id: String?
    var locationCapital: String?
    var locationCountry_flag: String?
    var locationCountry_flag_emoji: String?
    var locationCountry_flag_emoji_unicode: String?
    var locationCalling_code: String?
    var locationIs_eu: Bool
    var i: Int
    var count_i: Int
    var code: String?
    var name: String?
    var native: String?

    ip = json.string(of: "ip")
    v_type = json.string(of: "type")
    continent_code = json.string(of: "continent_code")
    continent_name = json.string(of: "continent_name")
    country_code = json.string(of: "country_code")
    country_name = json.string(of: "country_name")
    region_code = json.string(of: "region_code")
    region_name = json.string(of: "region_name")
    city = json.string(of: "city")
    zip = json.string(of: "zip")
    latitude = json.string(of: "latitude")
    longitude = json.string(of: "longitude")
    locationGeoname_id = json.string(of: "location.geoname_id")
    locationCapital = json.string(of: "location.capital")
    locationCountry_flag = json.string(of: "location.country_flag")
    locationCountry_flag_emoji = json.string(of: "location.country_flag_emoji")
    locationCountry_flag_emoji_unicode = json.string(of: "location.country_flag_emoji_unicode")
    locationCalling_code = json.string(of: "location.calling_code")
    locationIs_eu = json.bool(of: "location.is_eu")
    i = 0
    count_i = json.size(ofArray: "location.languages").intValue
    while i < count_i {
        json.i = i
        code = json.string(of: "location.languages[i].code")
        name = json.string(of: "location.languages[i].name")
        native = json.string(of: "location.languages[i].native")
        i = i + 1
    }


}