2011-11-11 33 views
7

tôi đang kiểm tra kết nối mạng trên viewDidLoad sử dụng này:Làm thế nào để phát hiện sự thay đổi trong mạng với Khả năng hiển thị?

-(BOOL)reachable { 
    ReachabilityDRC *r = [ReachabilityDRC reachabilityWithHostName:@"google.com"]; 
    NetworkStatus internetStatus = [r currentReachabilityStatus]; 
    if(internetStatus == NotReachable) { 
     return NO; 
    } 
    return YES; 
} 

Nhưng tôi cũng muốn được thông báo nếu có sự thay đổi của mạng, chẳng hạn như wifi giảm, hoặc wifi là trở lại, vì vậy tôi có thể làm cho thay đổi tương ứng.

Tôi có thể điều chỉnh phương pháp của mình để làm điều gì đó như thế?

+0

Bản sao có thể có của: http://stackoverflow.com/questions/784582/easiest-way-to-determine-whether-iphone-internet-connection-is-available – 0x8badf00d

Trả lời

6

Một giải pháp khả thi là thêm một thông báo NS trong "ứng dụng didfinishlaunching":

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkForReachability) name:kReachabilityChangedNotification object:nil]; 

và trong phương pháp checkForReachability làm điều này:

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

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus]; 

    if(remoteHostStatus == NotReachable) { 
     //Do something 
    } 
    else if (remoteHostStatus == ReachableViaWiFi) { 
    // Do something 
} 
    else{ 

// Else do something else 
} 
+1

Điều này sẽ không hoạt động như dự định. Khả năng tiếp cận initialisation và startNotifier nên đi vào appDidFinishLaunching, bởi vì checkForReachability sẽ không được gọi nếu bạn không startNotifier. –

0

Tôi đã sử dụng phần mở rộng tuyệt vời để các reachability lớp mà Donoho Design Group đã kết hợp với nhau. Nó có các thông báo cho phép bạn được cảnh báo khi trạng thái mạng thay đổi.

http://blog.ddg.com/?p=24

Chúc may mắn

9

1- thêm SystemConfiguration.framework để dự án của bạn.

2- Tải file từ sau GitHub

Reachability.h 
Reachability.m 

3- Thêm các tập tin trong dự án của bạn

4 thêm @class Reachability; trong YourViewController.h

#import <UIKit/UIKit.h> 

@class Reachability; 

5- thêm biến Reachability* internetReachable; trong YourViewController.h

#import <UIKit/UIKit.h> 

@class Reachability; 

@interface YourViewController : UIViewController { 
    Reachability* internetReachable; 
} 

6- thêm Reachability.h trong YourViewController.m

#import "YourViewController.h" 
#import "Reachability.h" 

7- add sau dòng trong -(void)ViewDidLoad trong YourViewController.m

-(void)ViewDidLoad { 
    [[NSNotificationCenter defaultCenter] 
         addObserver:self 
         selector:@selector(checkNetworkStatus:) 
         name:kReachabilityChangedNotification 
         object:nil]; 

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

8- add chức năng sau đây sau khi -(void)viewDidLoad

-(void) checkNetworkStatus:(NSNotification *)notice 
{ 
    // called after network status changes 
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; 
    switch (internetStatus) 
    { 
     case NotReachable: 
     { 
      NSLog(@"The internet is down."); 
      break; 
     } 
     case ReachableViaWiFi: 
     { 
      NSLog(@"The internet is working via WIFI."); 
      break; 
     } 
     case ReachableViaWWAN: 
     { 
      NSLog(@"The internet is working via WWAN."); 
      break; 
     } 
    } 
} 

Bây giờ bao giờ hết y thay đổi kết nối internet, bạn sẽ thấy bảng điều khiển đăng nhập.

+1

@ Salim, Woking như một say mê .... Cảm ơn người đàn ông –

Các vấn đề liên quan