Confused
Confused

Reputation: 3926

reachabilityWithHostName is not working

I want to confirm that an URL is accessible/reachable before calling it. I knew reachabilityWithHostName method can help me to do this task. I used the following code to know about URL reachability. But it does not works.

Reachability *connection = [Reachability reachabilityWithHostName:@"stackoverflow.com"];

[connection startNotifier];

NetworkStatus netStatus = [connection currentReachabilityStatus];

The netStatus always returns NotReachable(means 0) value even everything is fine.

Notes:

Help needed!! Confused!!

Upvotes: 3

Views: 823

Answers (1)

iOSfleer
iOSfleer

Reputation: 418

Use Reachability like here: https://github.com/tonymillion/Reachability

    Reachability *connection = [Reachability reachabilityWithHostName:@"stackoverflow.com"];
    connection.reachableBlock = ^(Reachability*reach)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            NetworkStatus netStatus = [reach currentReachabilityStatus];
            NSLog(@"network status: %ld", netStatus);
        });
    };
    connection.unreachableBlock = ^(Reachability*reach)
    {
        NetworkStatus netStatus = [reach currentReachabilityStatus];
        NSLog(@"network status: %ld", netStatus);
    };

    [connection startNotifier];

Upvotes: 1

Related Questions