我正在使用这个库:https://github.com/tonymillion/Reachability
有时当我检查以下表达式时会出现问题:
[[[ReachabilityManager sharedManager]reachability] isReachable];
在应该为 YES
时返回 NO
。我知道有互联网,因为我同时调用了 Rest 服务。
另外,如果我执行 [ReachabilityreachabilityWithHostname"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
@property (nonatomic) Reachability *hostReachability;
@property (nonatomic) Reachability *internetReachability;
@property (nonatomic) Reachability *wifiReachability;
声明一个 bool 值,
BOOL _isInternetAvaliable;
初始化您的可达性,
-(void) initializeReachability {
[[NSNotificationCenter defaultCenter] addObserver:self selectorselector(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)updateInterfaceWithReachabilityReachability *)reachability
{
if (reachability == self.hostReachability)
{
[self configurereachability:reachability];
}
if (reachability == self.internetReachability)
{
[self configurereachability:reachability];
}
if (reachability == self.wifiReachability)
{
[self configurereachability:reachability];
}
}
- (void)configurereachabilityReachability *)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
}
关于iOS - 可达性 isReachable 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42203724/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |