Skip to content

Instantly share code, notes, and snippets.

@LukeSmith16
Last active April 7, 2023 21:15
Show Gist options
  • Save LukeSmith16/9494a5bd6e57b4a66a8740508329f859 to your computer and use it in GitHub Desktop.
Save LukeSmith16/9494a5bd6e57b4a66a8740508329f859 to your computer and use it in GitHub Desktop.
func downloadHighResImage() {
var request = URLRequest(url: URL(string: "highQualityImageURL")!)
// Trigger error in `session.dataTask` if user has low data mode enabled.
request.allowsConstrainedNetworkAccess = false
URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error as? URLError,
error.networkUnavailableReason == .constrained {
// Download low res image which uses less data
downloadLowResImage()
}
// Logic...
}.resume()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment