How to return BOOL when checking internet connection in XCODE

坚强是说给别人听的谎言 提交于 2020-01-21 18:38:47

问题


I want to be able to check for internet connectivity when my View loads. To predetermine the contents of my view.

I have the following viewDidLoad method:

- (void)viewDidLoad {
    [super viewDidLoad];

    if(![self hasInternetConnection]){
        NSLog(@"SHOW ORIGINAL DOC");
    }
    else{
        NSLog(@"SHOW NEW DOC");
    }
}

And I have a method called hasInternetConnection as follows:

- (BOOL)hasInternetConnection{

    NSLog(@"Starting connection test");

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

    // Internet is reachable
    internetReachableFoo.reachableBlock = ^(Reachability*reach){
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"We have internet");
            return YES;
        });
    };

    // Internet is not reachable
    internetReachableFoo.unreachableBlock = ^(Reachability*reach){
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"We do not have internet");
            return NO;
        });
    };

    [internetReachableFoo startNotifier];

}

I don't want to use the deprecated Apple reachibility class using:

NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];

How can I change the code in -(BOOL)hasInternetConnection to efficiently return a boolean for my method to work?


回答1:


What i do in My Projects :

Create a custom class CheckInternet of type NSObject

in CheckInternet.h file

+ (BOOL) isInternetConnectionAvailable;

and in CheckInternet.m file

+ (BOOL) isInternetConnectionAvailable
{
Reachability *internet = [Reachability reachabilityWithHostName: @"www.google.com"];
NetworkStatus netStatus = [internet currentReachabilityStatus];
bool netConnection = false;
switch (netStatus)
{
    case NotReachable:
    {
        NSLog(@"Access Not Available");
        netConnection = false;
        break;
    }
    case ReachableViaWWAN:
    {
        netConnection = true;
        break;
    }
    case ReachableViaWiFi:
    {
        netConnection = true;
        break;
    }
}
return netConnection;
}

import this class to your desired class, Now you can access as

in viewDidLoad or any other method where you want

if ([CheckInternet isInternetConnectionAvailable])
{
    // internet available
}
else
{
    // no internet
 }



回答2:


Best way is to use Reachability code. Check here for apple sample code. That has a lot of convenience methods to check internet availability, Wifi/WAN connectivity check etc..

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkChanged:) name:kReachabilityChangedNotification object:nil];

reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];

- (void)networkChanged:(NSNotification *)notification
{

  NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

  if(remoteHostStatus == NotReachable) { NSLog(@"not reachable");}
  else if (remoteHostStatus == ReachableViaWiFiNetwork) { NSLog(@"wifi"); }
  else if (remoteHostStatus == ReachableViaCarrierDataNetwork) { NSLog(@"carrier"); }
}



回答3:


I think you can use the Apple provided Reachability class. There is a method: - (NetworkStatus)currentReachabilityStatus;

As it returns NetworkStatus. You can use this value directly or inside your function reachabilityStatus != NotReachable




回答4:


Yo can respect the asynchronicity in the implementation and modify the signature of the method to accept a block too.

First you typedef your new block in the header

typedef void(^ReachabilityCompletionBlock)(BOOL reachable);

then

- (void)isInternetReachable:(ReachabilityCompletionBlock)paramReachabilityBlock
{
    NSLog(@"Starting connection test");

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

    // Internet is reachable
    internetReachableFoo.reachableBlock = ^(Reachability*reach){
    // Update the UI on the main thread
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"We have internet");
        paramReachabilityBlock(YES); 
        });
    };

    // Internet is not reachable
    internetReachableFoo.unreachableBlock = ^(Reachability*reach){
    // Update the UI on the main thread
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"We do not have internet");
        paramReachabilityBlock(NO);
        });
    };

    [internetReachableFoo startNotifier];
}


来源:https://stackoverflow.com/questions/28854560/how-to-return-bool-when-checking-internet-connection-in-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!