Search code examples
swifthttp-erroropenurl

With Swift, is there a way to open URL and get the http error


I'm doing

if !UIApplication.shared.openURL(url) {         
    print("Error")
}

The issue is that I've the message error in the console and I'm seeking how to know what specific HTTP error is thrown.


Solution

  • You can get your responses by doing something like this.

    let URL = URL(string: "www.google.com")
    let task = URLSession.shared.dataTask(with:url!) { _, response, _ in 
         if let status = response as? HTTPURLResponse {
              print(status.statusCode)
         }
    }
    
    task.resume()
    

    Apologies for any syntax issues. No XCode available at the moment.