1

Do you know if I can simply test if there is a WIFI local connection ? For example if the url 192.168.0.100 is reachable. I tried with the Reachability without success. It tells me that it is connected, but it is not true.

I would like first to test if there is a local WIFI connection, and then when I am sure there is a connection, launch that Web Service :

- (void)callWebService:(NSString *)url withBytes:(NSString *) bytes //GET
{
        NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
        NSString *url_string = [bytes stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [request setURL:[NSURL URLWithString:[url stringByAppendingString: url_string]]];
        [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
        [request setTimeoutInterval:timeOut];
        NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; //try NSURLSession
        [connection start];
}

Thanks in advance.

ΩlostA
  • 2,501
  • 5
  • 27
  • 63
  • 192.168.0.100 isn't a URL. It's entirely possible that the address is reachable but that there's no HTTP service listening. – Phillip Mills Feb 27 '17 at 20:30
  • I know that address is returning 200, and if i put some that doesn't exist like 192.168.0.101 that returns me 404. But if i use Reachability, it returns me YES everytime. So I would like to know if there is a way to have that status code ? I tried with an asynchronous nsurl connection, it works, but the problem is that it doesn't execute the connectionDidFinishLoading => http://stackoverflow.com/questions/42490047/connectiondidfinishloading-not-called-with-the-use-of-nsurlconnection-sendasyn/42490402?noredirect=1#comment72129076_42490402 – ΩlostA Feb 27 '17 at 20:35

2 Answers2

2

To test for internet connection you must employ Apple's Reachability. Check for reachability with the ReachableViaWiFi enumeration.

Then you will need to do a ping of your server. In your didReceiveResponse method you need to search for a successful reach of your server.

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSInteger status = [httpResponse statusCode];

    if (status >= 200 && status <300)
    {
        // You are able to reach the server. Do something.
    }
}

EDITED

"I tried with the Reachability without success"

Did you happen to forget to notify reachability to startNotifier?

Reachability *reachability = [Reachability reachabilityWithHostname:@"www.google.com"];

reachability.reachableBlock = ^(Reachability *reachability) {
    NSLog(@"Network is reachable.");
};

reachability.unreachableBlock = ^(Reachability *reachability) {
    NSLog(@"Network is unreachable.");
};

// Start Monitoring
[reachability startNotifier];
Brandon A
  • 8,153
  • 3
  • 42
  • 77
  • Thank you, I believed that there was a way with the asynchronous method. Reachability is not a good method to check a Local status code 200 – ΩlostA Feb 27 '17 at 21:14
  • 1
    Yes reachability isn't going to tell you if your response is 200 or not. It is just a utility class to determine network connection so that you can make a call to get a response at all. – Brandon A Feb 27 '17 at 21:16
1

NSURLConection have many delegate methods. try one of the following:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [self.download_connection cancel]; // optional depend on what you want to achieve.
    self.download_connection = nil; // optional

    DDLogVerbose(@"Connection Failed with error: %@", [error description]);
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSInteger state = [httpResponse statusCode];

    if (state >= 400 && state < 600)
    {
        // something wrong happen.
        [self.download_connection cancel]; // optional
        self.download_connection = nil; // optional
    }
}
hasan
  • 23,815
  • 10
  • 63
  • 101