2017-02-13 34 views
2

我使用這個庫:https://github.com/tonymillion/Reachability的iOS - 可達isReachable不工作

的問題是,有時當我檢查下面的表達式: [[[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 
+1

嘗試使用apple的可達性類,您可以下載源代碼聽到https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html –

+0

朋友你只是檢查主機可達性? –

+0

我想測試互聯網連接。 – Ricardo

回答

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 
} 
+0

[[ReachabilityManager sharedManager] isInternetAvailable]始終使用此代碼返回YES。 – Ricardo

+0

我再次檢查了我的代碼,在我的項目中工作,檢查您是否連接到任何Wifi或WAN網絡。 –

+0

調試'configurereachability:'方法,如果你沒有連接到互聯網'netStatus'應該得到'NotReachable'並且它應該返回NO –

0

最後我找到了一個可靠的解決方案。 stackoverflow.com/a/14870229/2139691