Skip to content

Instantly share code, notes, and snippets.

@david8lumen
Last active May 5, 2021 15:34
Show Gist options
  • Save david8lumen/62d75f9fad6c63226ebc461dfc584c89 to your computer and use it in GitHub Desktop.
Save david8lumen/62d75f9fad6c63226ebc461dfc584c89 to your computer and use it in GitHub Desktop.
iOS HTTP Request
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
var components = URLComponents()
components.scheme = "http"
components.host = "192.168.1.53"
components.port = 8000
components.path = "/teachers.json"
// Which finally brings us http://192.168.1.53:8000/teachers.json
if let url = components.url {
URLSession.shared.dataTask(with: url) { data, response, error in
if let error = error {
print(error)
} else if let data = data,
let jsonResponse = try? JSONSerialization.jsonObject(with: data,
options: .allowFragments) {
print(jsonResponse)
}
}.resume()
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment