How can I find a server using the Reachability code on the iPhone?


I have a client server "http://abc.com" and I want to check if this server is responding or not.
How to check this with Apple's Reachability code?
As I see this code, but can not find where to write the URL of my client to verify this. Please suggest me.

+6
source share
2 answers

Here is a synchronous example. You probably want to do this asynchronously, but it will help you check quickly.

Reachability *netReach = [Reachability reachabilityWithHostName:@"host.name"]; //return [netReach currentReachabilityStatus]; NetworkStatus netStatus = [netReach currentReachabilityStatus]; if (netStatus==ReachableViaWiFi) { [ViewManager showStatus:@"Reachable (WiFi)!"]; } else if(netStatus==ReachableViaWWAN) { [ViewManager showStatus:@"Reachable (WWAN)!"]; } else { [ViewManager showStatus:@"Not reachable, aww :-("]; } 

When using reachability withHostName, you should remember that this is just the host name, there should be no http: // prefix or the like.

+9
source

To check the availability of your server using the Apple (Reachability) code, you should inspect its utilities, as shown below:

 + (Reachability*) reachabilityWithHostName: (NSString*) hostName; 

But, in my opinion, I will first check the reachability for a combination of other factors (check the 3G / Edge / Wifi Internet connection, and then check the required host availability) in the same instance. Only one of the safe checks inside the upgrade reachability method in my script.

  else if (((netStatus == ReachableViaWiFi) || (netStatus == ReachableViaWWAN)) && connectionRequired == YES) { isInternetConAvailable = ([[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:kReachibility_ping_uri] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0] delegate:self]) ? YES : NO; } 

Where the kReachibility_ping_uri "constant contains the host name, the reachability class will post a notification of" kReachabilityChangedNotification "whenever the availability change on the iphone client is at the same time as you to perform all reachability checks and update the reachability status.

If you need an example, as I used in my applications, then let me know, I provided the code.

0
source

Source: https://habr.com/ru/post/886877/


All Articles