2012-07-01 15 views
16

Downloaded reachability từ Apple, sử dụng phương pháp này để kiểm tra các kết nối đang hoạt động:reachability không làm việc như mong đợi

-(BOOL)isReachable{ 

    Reachability *r = [Reachability reachabilityWithHostName:@"http://www.google.com"]; 

    if(NotReachable == [r currentReachabilityStatus]) { 
     NSLog(@"Not Reachable"); 
     return NO; 
    } 

    NSLog(@"Reachable"); 
    return YES; 

} 

Returns NO mỗi lần duy nhất dù đã được kết nối? Không thể tìm ra lý do tại sao ...

Bất kỳ ý tưởng nào? Cảm ơn.

Một lưu ý phụ, bất kỳ ai cũng có thể giới thiệu một lớp singleton dễ tiếp cận tốt không?

Trả lời

30

EDITED: bạn nên loại bỏ các giao thức, http từ reachabilityWithHostName cuộc gọi của bạn, vì vậy cập nhật nó để

Reachability *reachability = [Reachability reachabilityWithHostname:@"www.google.com"]; 
NetworkStatus netStatus = [reachability currentReachabilityStatus]; 
+1

Yeh tôi đã làm được điều đó và có các bit thông báo làm việc tốt. Tôi muốn một phương pháp kiểm tra trạng thái khả năng kết nối trước khi thực hiện các tác vụ nhất định (chẳng hạn như tải xuống sản phẩm mua trong ứng dụng) –

+0

@AdamWaite xin lỗi tôi đã phạm sai lầm trước đây, tôi đã cập nhật câu hỏi –

+0

Tuyệt vời. Cảm ơn. –

1

tôi sử dụng KSReachability. Nó hoạt động với NSNotification, các khối, KVO, và có hoặc không có ARC.

KSReachability *reachability = [KSReachability reachabilityToHost:@"www.google.com"]; 
reachability.onReachabilityChanged = ^(KSReachability* reachability) { 
    NSLog(@"Update: network is %@", reachability.reachable ? @"up." : @"down."); 
}; 
1

Nếu bạn đang cố gắng xem thiết bị có thể truy cập Internet nói chung, bạn có thể nên sử dụng reachabilityForInternetConnection thay vì reachabilityWithHostName :. Ngoài ra, cả hai cuộc gọi này sẽ mất một chút thời gian để bắt đầu (nó sẽ vẫn còn trong mili giây nhưng dài hơn thời gian cần thiết để đạt được điều kiện if trên dòng tiếp theo.) Đây là một ví dụ về một lớp singleton sử dụng khả năng truy cập.

static NetworkManager* sharedInstance = nil; 

@interface NetworkManager() 
@property (nonatomic, strong) Reachability* reachability; 
@end 

@implementation NetworkManager 
@synthesize reachability; 

+ (NetworkManager*)sharedInstance 
{ 
    @synchronized(self) { 
     if (sharedInstance == nil) { 
      sharedInstance = [[NetworkManager alloc] init]; 
     } 
    } 
    return sharedInstance; 
} 

- (id)init 
{ 
    reachability = [WYReachability reachabilityForInternetConnection]; 
} 

- (BOOL)isNetworkReachable 
{ 
    return ([self.reachability currentReachabilityStatus] != NotReachable); 
} 
@end 

Để kiểm tra mạng có thể truy cập trong các lớp khác bạn có thể sử dụng.

[[NetworkManager sharedInstance] isNetworkReachable]; 
5

Sử dụng mã này để kiểm tra xem các thiết bị được kết nối với Internet hay không

sử dụng mã này trong viewDidLoad:

Reachability* internetReachable; = [Reachability reachabilityForInternetConnection]; 
    [internetReachable startNotifier]; 

    hostReachable = [Reachability reachabilityWithHostName: @"www.apple.com"] ; 
    [hostReachable startNotifier]; 

và thêm chức năng này để mã của bạn:

-(void) checkNetworkStatus:(NSNotification *)notice 
{ 
    recheabilityBool=FALSE; 
    nonrecheabilityBool=FALSE; 
    // called after network status changes 
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; 
    switch (internetStatus) 
    { 
     case NotReachable: 
     { 
      nonrecheabilityBool=TRUE; 
      internetCon=0; 
      //NSLog(@"The internet is down."); 


      break; 
     } 
     case ReachableViaWiFi: 
     { 
      NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
      internetCon=404; 
      [prefs setInteger:internetCon forKey:@"conKey"]; 

      //NSLog(@"The internet is working via WIFI."); 
      break; 

     } 
     case ReachableViaWWAN: 
     { 
      //NSLog(@"The internet is working via WWAN."); 

      break; 
     } 
    } 

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus]; 
    switch (hostStatus) 
    { 
     case NotReachable: 
     { 
      internetCon=0; 
      if(nonrecheabilityBool==FALSE) 
      { 
       //NSLog(@"A gateway to the host server is down."); 
      } 
      break; 

     } 
     case ReachableViaWiFi: 
     { 
      if(recheabilityBool==FALSE) 
      { 

       recheabilityBool=TRUE; 

       NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
       internetCon=404; 
       [prefs setInteger:internetCon forKey:@"conKey"]; 


       //NSLog(@"The internet is working via WIFI."); 
       break; 
      } 


      //NSLog(@"A gateway to the host server is working via WIFI."); 

      break; 
     } 
     case ReachableViaWWAN: 
     { 
      //NSLog(@"A gateway to the host server is working via WWAN."); 
      break; 
     } 
    } 
} 


- (BOOL)connected 
{ 
    Reachability *reachability = [Reachability reachabilityForInternetConnection]; 
    NetworkStatus networkStatus = [reachability currentReachabilityStatus]; 
    return !(networkStatus == NotReachable); 
} 
2

Tôi gặp vấn đề tương tự với Khả năng hiển thị của Tony Million: trạng thái mạng luôn được đặt thành Không tReachable. tôi sửa chữa nó bằng cách thêm các SystemConfigura Khung

Hy vọng nó giúp

+0

Điều này giúp giải quyết trường hợp của tôi, nhưng điều này nên được đưa vào bình luận – Raptor

0

Một câu trả lời hoàn chỉnh:

-(BOOL) isReachable:(NSString *)url 
{ 
    //Retrieve the host name by given url address. 
    NSString *hostName = [[NSURL URLWithString:url] host]; 
    Reachability *r = [Reachability reachabilityWithHostName:hostName]; 

    if(NotReachable == [r currentReachabilityStatus]) { 
     NSLog(@"Not Reachable"); 
     return NO; 
    } 
    NSLog(@"Reachable"); 
    return YES; 
} 
+0

Nó làm việc tốt ở lần đầu tiên. Tôi cần kiểm tra khả năng hiển thị của máy chủ trên mỗi hành động. Nhưng, luôn trả về khả năng hiển thị qua Wifi. Không phải máy chủ có thể truy cập được hay không. –

+0

theo tệp lớp Reachbilty của Apple, 'Khả năng hiển thị không thể cho ứng dụng của bạn biết nếu bạn có thể kết nối với một máy chủ cụ thể, chỉ có giao diện có thể cho phép kết nối và giao diện đó là WWAN.' [Apple Reachbilty] (https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007324-Intro-DontLinkElementID_2) –