Ios 在XCODE中检查internet连接时如何返回BOOL

Ios 在XCODE中检查internet连接时如何返回BOOL,ios,objective-c,iphone,xcode,reachability,Ios,Objective C,Iphone,Xcode,Reachability,我希望在加载视图时能够检查internet连接。预先确定我的观点的内容 我有以下viewDidLoad方法: - (void)viewDidLoad { [super viewDidLoad]; if(![self hasInternetConnection]){ NSLog(@"SHOW ORIGINAL DOC"); } else{ NSLog(@"SHOW NEW DOC"); } } 我有一个叫做hasInter

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

我有以下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 reachibility类使用:

NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];

如何更改-(BOOL)hasInternetConnection中的代码以有效地返回布尔值,使我的方法工作?

最好的方法是使用可达性代码。在这里查一查。它有很多方便的方法来检查互联网可用性、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"); }
}

我想你可以使用苹果提供的课程。有一种方法:-(NetworkStatus)currentReachabilityStatus


当它返回NetworkStatus时。您可以直接使用此值,也可以在函数
reachabilityStatus!=NotReachable

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

首先在页眉中键入定义新块

typedef void(^ReachabilityCompletionBlock)(BOOL reachable);
然后


我在项目中的工作:

创建NSObject类型的自定义类
检查Internet

检查internet.h
文件中

+ (BOOL) isInternetConnectionAvailable;
并在
检查internet.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;
}
将该类导入到所需的类中,现在可以作为

viewdiload
或任何其他需要的方法中

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

这是一个完美的解决方案,我应该将CheckInternet.h导入test.m还是test.h?在test.h或test.m中导入它,我想在这两种情况下都可以。。。。您可以检查它:Pglad以帮助您……:)@Zubair它是完美的,因为它消除了代码重复。也是第一次使用全局方法。我只是刚刚开始适应xcode的实践和目标c:)再次感谢!
if ([CheckInternet isInternetConnectionAvailable])
{
    // internet available
}
else
{
    // no internet
 }