菜鸟教程小白 发表于 2022-12-11 18:56:30

iOS - 可达性 isReachable 不起作用


                                            <p><p>我正在使用这个库:<a href="https://github.com/tonymillion/Reachability" rel="noreferrer noopener nofollow">https://github.com/tonymillion/Reachability</a> </p>

<p>有时当我检查以下表达式时会出现问题:
<code>[[reachability] isReachable];</code> 在应该为 <code>YES</code> 时返回 <code>NO</code>。我知道有互联网,因为我同时调用了 Rest 服务。 </p>

<p>另外,如果我执行 <code>;</code> 并请求方法 <code>isReachable</code> 则可以按预期工作。</p>

<p>我的测试很简单,在模拟器上试一下,然后插拔网线。</p>

<pre><code>@implementation ReachabilityManager

#pragma mark -
#pragma mark Default Manager
+ (ReachabilityManager *)sharedManager {
    static ReachabilityManager *_sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&amp;onceToken, ^{
      _sharedManager = [ init];
    });

    return _sharedManager;
}

#pragma mark -
#pragma mark Memory Management
- (void)dealloc {
    // Stop Notifier
    if (_reachability) {
      ;
    }
}

#pragma mark -
#pragma mark Class Methods
+ (BOOL)isReachable {
    return [[ reachability] isReachable];
}

+ (BOOL)isUnreachable {
    return ![[ reachability] isReachable];
}

+ (BOOL)isReachableViaWWAN {
    return [[ reachability] isReachableViaWWAN];
}

+ (BOOL)isReachableViaWiFi {
    return [[ reachability] isReachableViaWiFi];
}

#pragma mark -
#pragma mark Private Initialization
- (id)init {
    self = ;

    if (self) {
      // Initialize Reachability
      self.reachability = ;

      // Start Monitoring
      ;
    }

    return self;
}

@end
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><pre><code>@property (nonatomic) Reachability *hostReachability;
@property (nonatomic) Reachability *internetReachability;
@property (nonatomic) Reachability *wifiReachability;
</code></pre>

<p>声明一个 bool 值,</p>

<pre><code>BOOL _isInternetAvaliable;
</code></pre>

<p>初始化您的可达性,</p>

<pre><code>-(void) initializeReachability {
    [ addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

    //hostReachability
    NSString *remoteHostName = @&#34;www.apple.com&#34;;
    self.hostReachability = ;
    ;
    ;

    //internetReachability
    self.internetReachability = ;
    ;
    ;

    //wifiReachability
    self.wifiReachability = ;
    ;
    ;

}


- (void)updateInterfaceWithReachability:(Reachability *)reachability
{
    if (reachability == self.hostReachability)
    {
      ;
    }

   if (reachability == self.internetReachability)
   {
       ;
   }

   if (reachability == self.wifiReachability)
   {
       ;
   }
}


- (void)configurereachability:(Reachability *)reachability
    {
    NetworkStatus netStatus = ;
    BOOL 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;
}
</code></pre>

<p>现在您可以调用电话和访问互联网状态了,</p>

<pre><code>if([ isInternetAvailable]){
//Internet on
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iOS - 可达性 isReachable 不起作用,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42203724/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42203724/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS - 可达性 isReachable 不起作用