在 XCODE 中检查互联网连接时如何 return BOOL

How to return BOOL when checking internet connection in XCODE

我希望能够在我的视图加载时检查互联网连接。预先确定我的观点的内容。

我有以下 viewDidLoad 方法:

- (void)viewDidLoad {
    [super viewDidLoad];

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

我有一个名为 hasInternetConnection 的方法,如下所示:

- (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];

}

我不想使用已弃用的 Apple 可达性 class 使用:

NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];

如何更改 -(BOOL)hasInternetConnection 中的代码以有效地return 布尔值以使我的方法起作用?

最好的方法是使用可达性代码。在此处查看 apple sample code。那有很多方便的方法来检查互联网可用性,Wifi/WAN 连接检查等..

[[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"); }
}

我想你可以使用 Apple 提供的 Reachability class。有一种方法:- (NetworkStatus)currentReachabilityStatus;

作为 returns 网络状态。您可以直接使用这个值,也可以在函数内部使用 reachabilityStatus != NotReachable

你可以尊重实现中的异步性并修改方法的签名来接受块。

首先你在 header

中输入你的新块
typedef void(^ReachabilityCompletionBlock)(BOOL reachable);

然后

- (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];
}

我在我的项目中做了什么:

创建自定义 class CheckInternet 类型的 NSObject

CheckInternet.h 文件中

+ (BOOL) isInternetConnectionAvailable;

并在 CheckInternet.m 文件中

+ (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;
}

将此 class 导入到您想要的 class,现在您可以访问

viewDidLoad 或您想要的任何其他方法

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