service call in swift

Sep 04, 2021 Swift

How to Do Service Call wait until turn on the internet connectivity

 Swift Network Service call When internet is turned on ‘

Today We going to start with as Concept call of network service While internet is turn On.
 
Whenever the user turn Off the internet or wifi is connectivity is not available the App behave Static thus we need to give the popup of network connection to the user . As he turn On’s the internet the user need’s to refers the Screen of the Application Which reduce the User Experience.
 
whenever We use the URLSession  to make the Service call to get the Data response from the Api’s .if the user has disconnected the Internet the network throws the error. However if we Use the WaitConnectivity in the URLSession  and configuring it with true the network waits for Sometimes and call the Service.
 

Here We can Create the Wait Connectivity Service

 
 
let config = URLSessionConfiguration.default

config.waitsForConnectivity = true




URLSession(configuration: config).dataTask(with: "https://leadbycode.com") { data, response, error in

    if let error = error {

        print(error.localizedDescription)

    } 




    // use your data here

}.resume()
We can Also use the Default Session timeOut using the following code
 
 
public let k_DEFAULT_REQUEST_TIMEOUT = 180.0




let config = URLSessionConfiguration.default

config.timeoutIntervalForRequest = k_DEFAULT_REQUEST_TIMEOUT

URLSession(configuration: config).dataTask(with: "https://leadbycode.com") { data, response, error in

    if let error = error {

        print(error.localizedDescription)

    } 




    // use your data here

}.resume()
By this we can add the Wait for Connectivity session for the service call or wait session for the particular time interval
Index