작동하지 : [[[ReachabilityManager sharedManager] reachability] isReachable];
반환 NO
가 YES
해야한다 : 나는 다음 식을 검사 할 때 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
사과의 도달 가능성 클래스로 시도해보십시오. 소스 코드를 다운로드 할 수 있습니다. https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html –
친구 호스트 연결 가능성을 바로 확인하고 있습니까? –
인터넷 연결을 테스트하고 싶습니다. – Ricardo