2017-02-13 3 views
2

작동하지 : [[[ReachabilityManager sharedManager] reachability] isReachable]; 반환 NOYES해야한다 : 나는 다음 식을 검사 할 때 https://github.com/tonymillion/Reachability아이폰 OS - 도달 가능성의 isReachable 나는이 라이브러리를 사용하고

문제는 때때로이다. 그동안 인터넷에 휴식을 취하기 때문에 인터넷이 있다는 것을 압니다.

또한 [Reachability reachabilityWithHostname:@"www.google.com"];을 실행하고 isReachable 메서드를 요청하면 예상대로 작동합니다.

내 테스트는 간단합니다. 에뮬레이터로 시도한 다음 이더넷 케이블을 연결 및 분리하십시오.

-(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; 
} 

지금 당신이 & 액세스 인터넷 상태를 호출 할 준비가

@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

사과의 도달 가능성 클래스로 시도해보십시오. 소스 코드를 다운로드 할 수 있습니다. 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; 

,

if([[ReachabilityManager sharedManager] isInternetAvailable]){ 
    //Internet on 
} 
+0

[[ReachabilityManager sharedManager] isInternetAvailable]은 항상이 코드로 YES를 반환합니다. – Ricardo

+0

내 프로젝트에서 내 코드가 다시 작동하는지 확인하고, Wi-Fi 또는 WAN 네트워크에 연결되어 있지 않은지 확인하십시오. –

+0

당신이 인터넷에 연결되어 있지 않다면'configurereachability :'메소드를 디버깅하십시오'netStatus'는'NotReachable'을 가져야하고 NO를 돌려 주어야합니다 –

0

마지막으로 신뢰할 수있는 솔루션을 발견했습니다. stackoverflow.com/a/14870229/2139691