simple example for func urlSession(_ session: URLSession, taskIsWaitingForConnectivity task: URLSessionTask)

I have a button when click on it, save some information to firebase, but I want to wait (for example 1 minute) If the internet not available, and execute post if internet come back.

my code :




class AddReserve: UIViewController

{


var configuration = URLSessionConfiguration()

var session = URLSession()

var task = URLSessionTask()




override func viewDidLoad() {

super.viewDidLoad()

configuration = URLSessionConfiguration.default

configuration.allowsCellularAccess = true

configuration.timeoutIntervalForResource = 60 //seconds

if #available(iOS 11, *)

{

configuration.waitsForConnectivity = true

}

session = URLSession(configuration: configuration, delegate: self as? URLSessionDelegate, delegateQueue: nil)


}





@IBAction func buttonPost(_ sender: UIButton)

{


task = URLSession.shared.dataTask(with: URL(string: "https://www.google.com")!)

{ data, response, error in

self.addImage.titleLabel?.text = "starting.."

print("status waiting starting")

self.addImage.titleLabel?.textColor = .white

if data != nil

{

print("data \(String(describing: data))")

print("status waiting connected")


DispatchQueue.main.async()

{

// execute on main thread

postReserve()

print("status waiting completed")

}

}

else

{

print("status waiting no connect")

}

}


task.resume()


}



func urlSession(_ session: URLSession, taskIsWaitingForConnectivity task: URLSessionTask) {

// waiting for connectivity, update UI, etc.

print("status waiting")


}


func postReserve()

{

print("post sent successfully")

}

}

Replies

Did you try to set


configuration.timeoutIntervalForRequest = 60


But in fact, 60 is the default value.


So, what do you get now, and what do you expect ?


Note: when you post code, think of editing it properly (with <> formatter tool, and to skip useless blank lines.


class AddReserve: UIViewController {
   
    var configuration = URLSessionConfiguration()
    var session = URLSession()
    var task = URLSessionTask()
   
    override func viewDidLoad() {
        super.viewDidLoad()
        configuration = URLSessionConfiguration.default
        configuration.allowsCellularAccess = true
        configuration.timeoutIntervalForResource = 60 //seconds
        if #available(iOS 11, *)
        {
            configuration.waitsForConnectivity = true
        }
        session = URLSession(configuration: configuration, delegate: self as? URLSessionDelegate, delegateQueue: nil)
       
    }
   
    @IBAction func buttonPost(_ sender: UIButton) {
       
        task = URLSession.shared.dataTask(with: URL(string: "https://www.google.com")!)
        { data, response, error in
            self.addImage.titleLabel?.text = "starting.."
            print("status waiting starting")
            self.addImage.titleLabel?.textColor = .white
            if data != nil {
                print("data \(String(describing: data))")
                print("status waiting connected")
               
                DispatchQueue.main.async() {
                        // execute on main thread
                        self.postReserve()
                        print("status waiting completed")
                }
            } else {
                print("status waiting no connect")
            }
        }
       
        task.resume()
       
    }
      
    func urlSession(_ session: URLSession, taskIsWaitingForConnectivity task: URLSessionTask) {
        // waiting for connectivity, update UI, etc.
        print("status waiting")
     }
   
    func postReserve()  {
        print("post sent successfully")
    }

}


When editing your code I noted:

line 24 and 26: undefined addImage (probably just a property you did not show in the copied code)

line 33: self was missing


PS: you have a lot of open posts. Please close those where you got correct answer or feedback on remaining problem.

You create a session in

viewDidLoad
but you don’t use it in
buttonPost(_:)
. That method is still using the shared session (
URLSession.shared
).

I’ve made this mistake many times )-:

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I want more explain about your reply, plaese.

With reference to the code in you first post, you initially create a session with this line:

var session = URLSession()

You then replace that with a new session, with a custom configuration, with this line:

session = URLSession(configuration: configuration, delegate: self as? URLSessionDelegate, delegateQueue: nil)

But all of that is irrelevant because on this line:

task = URLSession.shared.dataTask(with: URL(string: "https://www.google.com")!)

you use

URLSession.shared
, not the session you created. As you’re not using your session, the delegate you set up for you session is never called.

To actually use your session you need to do this this:

task = self.session.dataTask(with: URL(string: "https://www.google.com")!)

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"