我正在使用此库:https://github.com/tonymillion/Reachability
当我检查以下表达式时,问题有时会出现:
[[[ReachabilityManager sharedManager] reachability] isReachable];
应该NO
时返回YES
。我知道有互联网,因为我在此期间调用Rest服务。
另外,如果我执行[Reachability reachabilityWithHostname:@"www.google.com"];
并要求方法isReachable
,那么按预期工作。
我的测试很简单,尝试使用仿真器,然后插拔以太网电缆。
@implementation ReachabilityManager
#pragma mark -
#pragma mark Default Manager
+ (ReachabilityManager *)sharedManager {
static ReachabilityManager *_sharedManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedManager = [[self alloc] init];
});
return _sharedManager;
}
#pragma mark -
#pragma mark Memory Management
- (void)dealloc {
// Stop Notifier
if (_reachability) {
[_reachability stopNotifier];
}
}
#pragma mark -
#pragma mark Class Methods
+ (BOOL)isReachable {
return [[[ReachabilityManager sharedManager] reachability] isReachable];
}
+ (BOOL)isUnreachable {
return ![[[ReachabilityManager sharedManager] reachability] isReachable];
}
+ (BOOL)isReachableViaWWAN {
return [[[ReachabilityManager sharedManager] reachability] isReachableViaWWAN];
}
+ (BOOL)isReachableViaWiFi {
return [[[ReachabilityManager sharedManager] reachability] isReachableViaWiFi];
}
#pragma mark -
#pragma mark Private Initialization
- (id)init {
self = [super init];
if (self) {
// Initialize Reachability
self.reachability = [Reachability reachabilityWithHostname:@"www.google.com"];
// Start Monitoring
[self.reachability startNotifier];
}
return self;
}
@end
答案 0 :(得分:0)
@property (nonatomic) Reachability *hostReachability;
@property (nonatomic) Reachability *internetReachability;
@property (nonatomic) Reachability *wifiReachability;
宣布一个布尔,
BOOL _isInternetAvaliable;
初始化你的可达性,
-(void) initializeReachability {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
//hostReachability
NSString *remoteHostName = @"www.apple.com";
self.hostReachability = [Reachability reachabilityWithHostName:remoteHostName];
[self.hostReachability startNotifier];
[self updateInterfaceWithReachability:self.hostReachability];
//internetReachability
self.internetReachability = [Reachability reachabilityForInternetConnection];
[self.internetReachability startNotifier];
[self updateInterfaceWithReachability:self.internetReachability];
//wifiReachability
self.wifiReachability = [Reachability reachabilityForLocalWiFi];
[self.wifiReachability startNotifier];
[self updateInterfaceWithReachability:self.wifiReachability];
}
- (void)updateInterfaceWithReachability:(Reachability *)reachability
{
if (reachability == self.hostReachability)
{
[self configurereachability:reachability];
}
if (reachability == self.internetReachability)
{
[self configurereachability:reachability];
}
if (reachability == self.wifiReachability)
{
[self configurereachability:reachability];
}
}
- (void)configurereachability:(Reachability *)reachability
{
NetworkStatus netStatus = [reachability currentReachabilityStatus];
BOOL connectionRequired = [reachability connectionRequired];
switch (netStatus)
{
case NotReachable: {
_isInternetAvaliable = NO;
break;
}
case ReachableViaWWAN: {
_isInternetAvaliable = YES;
break;
}
case ReachableViaWiFi: {
_isInternetAvaliable = YES;
break;
}
}
if (connectionRequired)
{
_isInternetAvaliable = NO;
}
}
-(BOOL) isInternetAvailable {
return _isInternetAvaliable;
}
现在你已准备好打电话给&访问互联网状态,
if([[ReachabilityManager sharedManager] isInternetAvailable]){
//Internet on
}
答案 1 :(得分:0)
最后我找到了一个可靠的解决方案。 https://stackoverflow.com/a/14870229/2139691