Tutorial :ping to web server from iphone



Question:

I am developing an aplication which needs to download some data from webserver.So i need to first ping to the web server to check whether the service is available after checking the internet connectivity .I used the following code to check server availability

BOOL success = NO;  const char *host_name = [@"http://192.168.1.7:8080/TestWeb/webresources/"                               cStringUsingEncoding:NSASCIIStringEncoding];    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL,                                                                                  host_name);  SCNetworkReachabilityFlags flags;  success = SCNetworkReachabilityGetFlags(reachability, &flags);  BOOL isAvailable = success && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);  return isAvailable;  

where my server is uploaded locally.But this code always return an invalid result,but it works correctly if i gave some exixting sites like google.com.Please let me know whats i am doing wrong..


Solution:1

I would use NSURLConnection to ping the server. If the connection can't be made, you will get the response in the delegate method connectionDidFailWithError.

Check out the Apple documentation, they have a good code example: link text


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »