如果 google.com 不起作用!但是网络还可以。代码会带来错误。所以我想删除“google.com”来检测 net 的状态。
我找不到没有地址的可达性方法。
#import "Reachability.h"
static ReachabilityCenter *reachCenter;
@implementation ReachabilityCenter
@synthesize reachablity;
@synthesize isConnectedNet;
+(id)shareCenter
{
if(!reachCenter)
{
reachCenter = [[ReachabilityCenter alloc] init];
reachCenter.reachablity = [Reachability reachabilityWithHostname:@"www.google.com"];
}
return reachCenter;
}
-(void)reachabilityChanged:(NSNotification*)note
{
Reachability * reach = [note object];
isConnectedNet = [reach isReachable];
}
-(void)beginListening
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
[reachCenter.reachablity startNotifier];
}
@end
我测试代码检测没有网站,ip 的状态。
-(void)detectNetStatus
{
Reachability *r = [Reachability reachabilityForLocalWiFi];
isConnectedNet = [r currentReachabilityStatus];
}
-(NetworkStatus)isConnectedNet
{
[self detectNetStatus];
return isConnectedNet;
}