• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iOS - 可达性 isReachable 不起作用

[复制链接]
菜鸟教程小白 发表于 2022-12-11 18:56:30 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在使用这个库: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



Best Answer-推荐答案


@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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap