-1

I was thinking to check my URL before doing a web service call, if there is an missing page or a server down, the URL will not be accessible for exemple, if I put a wrong URL i get this:

NSURLErrorDomain Code=-1003

So I wan't to check an URL before calling my SOAP webService, and I want to get the status code, if the status equals to 200 I will do the call, someone know how to do that ?

Ben
  • 761
  • 1
  • 12
  • 35
  • 1
    I don't understand why you would need to make a separate call? If your webservice is unreachable due to network issues or whatever, you won't get a 200 anyway. – Jacob King Oct 11 '16 at 07:25
  • If you *get this* you're actually checking the URL. Just handle the error properly. – vadian Oct 11 '16 at 07:26
  • @JacobKing I need to use this fonction on another class like rss parser – Ben Oct 11 '16 at 08:18
  • @vadian I need to use the function another time – Ben Oct 11 '16 at 08:19

4 Answers4

0

from apple documentation, I have found some information and guess this will help you. https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html

You can test them respectively by starting a notifier with this :

let wifiReachability = Reachability. reachabilityForLocalWiFi()
wifiReachability.startNotifier()

let internetReachability = Reachability.reachabilityForInternetConnection()
hostReachability.startNotifier()

let hostReachability = Reachability(hostName:"www.apple.com")
hostReachability.startNotifier()
Litle Dev
  • 474
  • 1
  • 5
  • 18
0

Swift 3 Usually I use this to check connection and speed:

    let url = URL(string: "http://......jpg") // image on web server to check the speed
    let request = URLRequest(url: url!)

    let session = URLSession.shared        

    let startTime = Date()

    let task =  session.dataTask(with: request) { (data, resp, error) in

        guard error == nil && data != nil else{



            print("connection error or data is nill\(error)")

            return
        }

        guard resp != nil else{

            print("respons is nill")
            return
        }




            let length  = CGFloat( (resp?.expectedContentLength)!) / 1000000.0

            print(length)



        let elapsed = CGFloat( Date().timeIntervalSince(startTime))

        print("elapsed: \(elapsed)")

        print("Speed: \(length/elapsed) Mb/sec")
Rob
  • 2,649
  • 3
  • 27
  • 34
0

Swift 4

Hi, I don't know if this is applicable but I have a work around on this one.

Inside in your URLSession method with (data, response, err), I use the err because it contains the information in the localizedDescription with the word "Could not connect to the server.". From that I grab that information and use it to check if you are connected to the server.

if err?.localizedDescription.range(of: "Could not connect to the server.") != nil { print("Could not connect to the server!") } else if err?.localizedDescription.range(of: "A server with the specified hostname could not be found.") != nil { print("A server with the specified hostname could not be found.") }

Hope, this will help.

Alvin Quezon
  • 179
  • 4
  • 15
0

By using the protocol of this NSURLConnectionDataDelegate

  • you can get the Status code from below delegate,
  • if you get statusCode = 503 then server is down
  • if you get StatusCode = 500 then Server is crashed on your Api or internal server error.
  • if you get statusCode = 200 then you get Success reply form
 func connection(_ connection: NSURLConnection, didReceive response: URLResponse)
    {
        let httpRes = response as! HTTPURLResponse

        print("http service response is \(httpRes)")

        statusCode = httpRes.statusCode
        switch (httpRes.statusCode) {
        case 201, 200, 401,503:
            // do what you want
        default:
            print("ignore")
        }
    }
Sylhare
  • 5,907
  • 8
  • 64
  • 80