0

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.

Jon Bell
  • 17
  • 5
  • This could be what you're looking for.. https://stackoverflow.com/questions/23428793/nsurlsession-how-to-increase-time-out-for-url-requests – Declan Conway May 28 '17 at 22:13
  • I believe you are correct however I was unable to implement this successfully. The above commented code is the same(I believe) as that link suggests. – Jon Bell Jun 01 '17 at 05:01

1 Answers1

0

Three thoughts:

  • You can check to see if the user is on the correct Wi-Fi network using the CNCopyCurrentNetworkInfo function in Core Foundation. There's no need to wait for a request to fail if the user is on the wrong network.
  • I'm assuming that you are having stalls caused by cellular networking taking over a request that should be sent to a (possibly disconnected) Wi-Fi network. Try disabling cellular networking at the request or session level, and lots of things will probably start magically working.
  • Instead of making the network request time out (which kills a connection that might still succeed if the network is dodgy), you should use an ordinary timer (NSTimer) to make visible some sort of passive UI indicator that says "Network Connection Error" (without forcing the user to interact with that indicator). This tells the user that something might be wrong without causing failures if the network is just slow for some reason (high packet loss from multipath interference, microwave oven, etc.)

In your case, because the network could be changing, you should probably periodically start additional network requests in parallel with the first, and cancel the original requests after you have more than two or three of them running. You can also use CNCopyCurrentNetworkInfo—or better yet, reachability—to determine when the network changes, and wait to schedule the first request until you've joined a network.

dgatwood
  • 10,129
  • 1
  • 28
  • 49
  • The CNCopyCurrentNetworkInfo is a neat trick but I don't believe it will work in this situation as the network ID is not static. (That is actually one function of this ap, to change the ssid and psk). Let me see if I can explain what I'm trying to do further.. – Jon Bell Jun 01 '17 at 04:54
  • In that case, I would suggest the third approach above. Show some text like "Unable to reach router. Are you on the right network? (Still trying.)", and trigger it using an ordinary NSTimer, and leave the network request alone until it either succeeds, fails, or the user cancels it. Optionally, start *additional*, parallel requests with an exponential backoff using that timer. – dgatwood Jun 01 '17 at 05:29
  • BTW, if you're setting the SSID, why don't you know the expected SSID? It should either be the previous SSID or the new one you're setting, shouldn't it? – dgatwood Jun 01 '17 at 05:33
  • I will look into using an NSTimer as you suggested in your third point. The SSID will be random on first boot so I do not know the default SSID. I may be able to work around this though. After the user changes the SSID it will be known so that could work after first change. – Jon Bell Jun 02 '17 at 04:49