ipinfo/swift
This is the official Swift client library for the [IPinfo.io](https://ipinfo.io) IP address API, allowing you to look up your own IP address, or get any of the following details for other IP addresses:
Installation
### Swift Package Manager
The [Swift Package Manager](https://swift.org/package-manager/) is a tool for automating the distribution of Swift code and is integrated into the `swift` compiler.
Once you have your Swift package set up, adding ipinfo as a dependency is as easy as adding it to the `dependencies` value of your `Package.swift`.
```swift
dependencies: [
.package(url: "https://github.com/ipinfo/swift", .upToNextMajor(from: "0.1.0"))
]
```
# Authentication
The IPinfo Swift library can be authenticated with your IPinfo API access token, which is passed with this key `IPInfoKitAccessToken` in `info.plist` file. Your IPinfo access token can be found in the account section of IPinfo's website after you have signed in: https://ipinfo.io/account/token
# IP Information
```swift
IPINFO.shared.getDetails(ip: "39.53.87.37") { status, data, msg in
switch status {
case .success:
print(data)
case .failure:
print(msg)
}
}
```
# ASN
```swift
IPINFO.shared.getASNDetails(asn: "AS13335") { status, data, msg in
switch status {
case .success:
print(data)
case .failure:
print(msg)
}
}
```
# Bulk
```swift
let ipAddresses = ["AS123", "8.8.8.8", "9.9.9.9/hostname", "2001:0:c000:200::0:255:1", "0.0.0.0"]
IPINFO.shared.getBatch(ipAddresses: ipAddresses, withFilter: false) { status, response, msg in
switch response {
case .success:
guard let response else { return }
if let ansResponse = response["AS123"] as? ASNResponse{
print(ansResponse)
}
if let ipResponse = response["8.8.8.8"] as? IPResponse{
print(ipResponse)
}
if let ipHostname = response["9.9.9.9/hostname"] as? String{
print(ipHostname)
}
if let batchIPV4 = response["0.0.0.0"] as? IPResponse{
print(batchIPV4)
}
if let batchIPV6 = response["2001:0:c000:200::0:255:1"] as? IPResponse{
print(batchIPV6)
}
case .failure:
print(msg)
}
}
```
# Country Name Lookup
This library provides a system to lookup country names through ISO2 country codes.
```swift
IPINFO.shared.getDetails(ip: "8.8.8.8") { status, response, msg in
guard let response else {return}
switch status {
case .success:
guard let response else { return }
// Print out the country code
print(response.country)
// Print out the country name
print(response.countryName)
case .failure:
print(msg)
}
}
```
# EU Country Lookup
This library provides a system to lookup if a country is a member of the European Union (EU) through ISO2 country codes.
```swift
IPINFO.shared.getDetails(ip: "8.8.8.8") { status, response, msg in
guard let response else {return}
switch status {
case .success:
// Print out whether the country is a member of the EU
print(response.isEU ?? false)
case .failure:
print(msg)
}
}
```
# Internationalization
This library provides a system to lookup if a country is a member of the European Union (EU), emoji and unicode of the country's flag, code and symbol of the country's currency, and public link to the country's flag image as an SVG and continent code and name through ISO2 country codes.
```swift
IPINFO.shared.getDetails(ip: "8.8.8.8") { status, response, msg in
guard let response else {return}
switch status {
case .success:
// Print out whether the country is a member of the EU
print(response.isEU ?? false)
// CountryFlag{emoji='🇺🇸',unicode='U+1F1FA U+1F1F8'}
print(response.getCountryFlag ?? CountryFlag(emoji: "🇺🇸", unicode: "U+1F1FA U+1F1F8"))
// https://cdn.ipinfo.io/static/images/countries-flags/US.svg
print(response.getCountryFlagURL ?? "https://cdn.ipinfo.io/static/images/countries-flags/US.svg")
// CountryCurrency{code='USD',symbol='$'}
print(response.getCountryCurrency?.code ?? "USD")
print(response.getCountryCurrency?.symbol ?? "$")
// Continent{code='NA',name='North America'}
print(response.getContinent?.code ?? "NA")
print(response.getContinent?.name ?? "North America")
case .failure:
print(msg)
}
}
```
# Location Information
This library provides an easy way to get the latitude and longitude of an IP Address:
```swift
IPINFO.shared.getDetails(ip: "8.8.8.8") { status, response, msg in
guard let response else {return}
switch status {
case .success:
// Print out the Latitude and Longitude
print(response.getLatitude ?? "")
print(response.getLongitude ?? "")
case .failure:
print(msg)
}
}
```
# Lite API
The library gives the possibility to use the [Lite API](https://ipinfo.io/developers/lite-api) too, authentication with your token is still required.
The returned details are slightly different from the Core API.
```swift
let client = IPInfoLite(token: "YOUR TOKEN")
let response = try await client.lookup(ip: "1.1.1.1")
switch response {
case .bogon(let bogon):
print(bogon.ip) // 192.168.2.1
case .ip(let ip):
print(ip)
/*
IPInfoLite.IPResponse(
ip: "1.1.1.1",
asn: "AS13335",
asName: "Cloudflare, Inc.",
asDomain: "cloudflare.com",
countryCode: "AU",
country: "Australia",
continentCode: "OC",
continent: "Oceania"
)
*/
}
```
# Core API
The library also supports the [Core API](https://ipinfo.io/developers/data-types#core-data), which provides city-level geolocation with nested geo and AS objects. Authentication with your token is required.
```swift
let client = IPInfoCore(token: "YOUR TOKEN")
let response = try await client.lookup(ip: "8.8.8.8")
print(response.ip) // 8.8.8.8
print(response.geo.city) // Mountain View
print(response.geo.country) // United States
print(response.as.asn) // AS15169
print(response.as.name) // Google LLC
```
# Plus API
The library also supports the [Plus API](https://ipinfo.io/developers/data-types#plus-data), which provides enhanced data including mobile carrier info and privacy detection. Authentication with your token is required.
```swift
let client = IPInfoPlus(token: "YOUR TOKEN")
let response = try await client.lookup(ip: "8.8.8.8")
print(response.ip) // 8.8.8.8
print(response.geo.city) // Mountain View
print(response.mobile) // mobile carrier info
print(response.anonymous.isProxy) // false
```
# Residential Proxy API
The library also supports the [Residential Proxy API](https://ipinfo.io/developers/residential-proxy-api), which allows you to check if an IP address is a residential proxy. Authentication with your token is required.
```swift
IPINFO.shared.getResproxyDetails(ip: "175.107.211.204") { status, data, msg in
switch status {
case .success:
print(data?.ip) // 175.107.211.204
print(data?.lastSeen) // 2025-01-20
print(data?.percentDaysSeen) // 0.85
print(data?.service) // Bright Data
case .failure:
print(msg)
}
}
```
# ContributingRunning the tests
Some tests require a token to pass. You can add yours as an environment variable of the scheme.
Other Libraries
There are official IPinfo client libraries available for many languages including PHP, Python, Go, Java, Ruby, and many popular frameworks such as Django, Rails, and Laravel. There are also many third-party libraries and integrations available for our API.
About IPinfo
Founded in 2013, IPinfo prides itself on being the most reliable, accurate, and in-depth source of IP address data available anywhere. We process terabytes of data to produce our custom IP geolocation, company, carrier, VPN detection, hosted domains, and IP type data sets. Our API handles over 40 billion requests a month for 100,000 businesses and developers.
[[image]](https://ipinfo.io/)
Package Metadata
Repository: ipinfo/swift
Default branch: main
README: README.md