0

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Pandruz
  • 345
  • 5
  • 18
  • You are making synchronous call .. make it async and then you can set a timeout – davidev Dec 21 '20 at 00:46
  • 1
    Does this answer your question https://stackoverflow.com/questions/64854765/loading-image-from-remote-url-asynchronously-in-swiftui-image-using-combines-pu? – Asperi Dec 21 '20 at 05:02
  • @davidev I don't understand how I can do that... – Pandruz Dec 22 '20 at 00:03
  • @Asperi Kind of, yes. I think I have to set a Session from what I read on that question you linked – Pandruz Dec 22 '20 at 00:03

0 Answers0