Skip to content

Instantly share code, notes, and snippets.

@sweeneyapps
Created July 7, 2021 19:24
Show Gist options
  • Save sweeneyapps/54c36ef6bb0c19eb0a12be31ead584aa to your computer and use it in GitHub Desktop.
Save sweeneyapps/54c36ef6bb0c19eb0a12be31ead584aa to your computer and use it in GitHub Desktop.
import Foundation
func loadWebsite(url: URL?) {
let group = DispatchGroup()
group.enter()
let task = URLSession.shared.dataTask(with: url!) { data, response, error in
if let error = error {
print(error)
group.leave()
return
}
guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
print("The httpResponse is not 2xx")
group.leave()
return
}
if let mimeType = httpResponse.mimeType, mimeType == "text/html",
let data = data,
let string = String(data: data, encoding: .utf8) {
print(string) // print out website's html on the terminal
group.leave()
}
}
task.resume()
group.wait()
}
loadWebsite(url: URL(string: "https://sweeneyapps.com"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment