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

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

我有以下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可达性类:

 NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; 

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

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

创建NSObject类型的自定义类CheckInternet

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

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

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

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

最好的方法是使用Reachability代码。 点击此处查看苹果示例代码 。 这有很多方便的方法来检查互联网可用性,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类。 有一种方法: – (NetworkStatus)currentReachabilityStatus;

因为它返回NetworkStatus。 您可以直接或在函数reachabilityStatus != NotReachable使用此值reachabilityStatus != NotReachable

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

首先,在标题中键入新块

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