I have an Image that is set based on an URL that the user can input. However, I would like to give it a timer so that if too much time passes (meaning either the website is down or the URL is written wrong) the download gets interrupted and it shows an Alert explaining what happened. My code is this at the moment:
@State var valueIcon : UIImage?
func getIcon(url: String) {
let endfav = "/favicon.ico"
let endWithoutSlash = "favicon.ico"
var websiteReady = ""
if url.last == "/" {
websiteReady = url + endWithoutSlash
} else {
websiteReady = url + endfav
}
let imageUrl = URL(string: websiteReady)!
let image = try? UIImage(withContentsOfUrl: imageUrl)
valueIcon = image
}
How can I add a timeout? And where do I insert it?