Created
July 7, 2021 19:24
-
-
Save sweeneyapps/54c36ef6bb0c19eb0a12be31ead584aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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