I am trying to Set the URLSession Timout Interval to lets say 5 seconds. The web page is a local page so it should be loaded in that amount of time. If the webpage is not loaded in 5 seconds I want an alert message to popup and tell the user to connect to the network. My question is, how can I implement this with the working code that I already have? I have tried to use a few different examples (One is commented out) but no luck. Any help is appreciated:)
func loadINFO(){
let urlString = "mytestPHP.php"
let url = URL(string: urlString)
//Try To Set Timout Interval...
//let urlconfig = URLSessionConfiguration.default
//urlconfig.timeoutIntervalForRequest = 5
//urlconfig.timeoutIntervalForResource = 5
//let session = URLSession(configuration: urlconfig)
URLSession.shared.dataTask(with:url!) { (data, response, error ) in
if error != nil {
//Alert User That They Are Not Connected To The Device
self.CreateAlert(title: "Error!", message: "Please Connect To Network and Try Again")
print(error as Any)
} else {
do {
let parsedData = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]
if let ssid = parsedData["SSID"] as? String,
let pass = parsedData["PASS"] as? String {
DispatchQueue.main.async {
// Update UI
self.SSID_INPUT.text = (ssid)
self.PASS_INPUT.text = (pass)
}
}
}
catch let error as NSError {
print(error)
}
}
}.resume()
}
Still haven't gotten an answer to this.. if anyone has a better solution I'm all ears.
Edit: Just to clarify a bit further. The php web server is running on the device in which I am changing the ssid and psk. So if the user is not on the correct network I would like them to receive my alert. The thing is, the ssid is not static so I cannot check if they're connected to a specific string value. That is why I was looking to see if I could lower the default session timeout interval and tell (after a few seconds) if they are on the network by seeing if they can reach the php webpage.