gpt4 book ai didi

iOS - 可达性 isReachable 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:14:34 29 4
gpt4 key购买 nike

我正在使用这个库:https://github.com/tonymillion/Reachability

问题有时出现在我检查以下表达式时:[[[ReachabilityManager sharedManager] reachability] isReachable]; 在应该为 YES 时返回 NO。我知道有互联网,因为我同时调用了 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

最佳答案

@property (nonatomic) Reachability *hostReachability;
@property (nonatomic) Reachability *internetReachability;
@property (nonatomic) Reachability *wifiReachability;

声明一个 bool 值,

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
}

关于iOS - 可达性 isReachable 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42203724/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com