Validate and Get Location Information from an IP Address in Swift

Did you know that you can learn a lot about users by checking out their IP address? That little string of numbers that composes an IP address contains a wealth of information that can be used to better understand your client base, as well as identify potential threats. In the following tutorial, we will demonstrate how you can use an API in Swift to extract data from an IP address. It’s a simple process that can provide valuable results.

All you’ll need to perform the validation is the IP address and your API key; if you need to retrieve your API key, you can quickly register for a free account on the Cloudmersive website.

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
var semaphore = DispatchSemaphore (value: 0)let parameters = "\"<string>\""
let postData = .utf8)
var request = URLRequest(url: URL(string: "")!,timeoutInterval: Double.infinity)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("YOUR-API-KEY-HERE", forHTTPHeaderField: "Apikey")
request.httpMethod = "POST"
request.httpBody = postData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
print(String(data: data, encoding: .utf8)!)

And voila! You will receive an instant result indicating if the IP address is a known threat, bot, or Tor exit node, as well as location information such as country name/code, city, currency, latitude, longitude, and more.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store