2013-07-23 21 views
9

Hi Tôi muốn chụp bất cứ khi nào người dùng được một kết nối mạng trong ứng dụng của tôi cho táo reachability lớp tôi đã thêm này và dưới đây là đoạn tôi đang sử dụng trong appdelegate phương pháp didFinishLaunchingWithOptions lớp học của tôi,Làm cách nào để thay đổi thông báo kết nối mạng từ Lớp Khả năng hiển thị iOS?

Reachability* reachability = [Reachability reachabilityForInternetConnection]; 
     [reachability startNotifier]; 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; 

và chọn reachabilityChanged của tôi phương thức như sau

- (void)reachabilityChanged:(NSNotification*)notification 
{ 
    Reachability* reachability = notification.object; 
    if(reachability.currentReachabilityStatus == NotReachable) 
     NSLog(@"Internet off"); 
    else 
     NSLog(@"Internet on"); 
} 

nhưng ở đây tôi không nhận được bất kỳ thông báo nào khi tắt chế độ Máy bay và khi tôi có kết nối mạng trong điện thoại.

Tôi có thiếu gì không?

+0

Kiểm tra http://stackoverflow.com/a/10184617/1635315 này. Điều này thực sự sẽ giúp bạn. – iKT

Trả lời

6

Có lẽ bạn nên thêm các quan sát viên trước khi startnotifier

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(handleNetworkChange:) name: kReachabilityChangedNotification object: nil]; 
reachability = [Reachability reachabilityForInternetConnection]; 
[reachability startNotifier]; 
13

tôi sử dụng một biến trong appdelegate để lưu trữ tình trạng mạng hiện tại như một bool

@property (nonatomic, assign) BOOL hasInet; 

.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    [self setUpRechability]; 
} 


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

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

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus]; 

    if   (remoteHostStatus == NotReachable)  {NSLog(@"no");  self.hasInet-=NO; } 
    else if  (remoteHostStatus == ReachableViaWiFi) {NSLog(@"wifi"); self.hasInet-=YES; } 
    else if  (remoteHostStatus == ReachableViaWWAN) {NSLog(@"cell"); self.hasInet-=YES; } 

} 

- (void) handleNetworkChange:(NSNotification *)notice 
{ 
    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus]; 

    if   (remoteHostStatus == NotReachable)  {NSLog(@"no");  self.hasInet-=NO; } 
    else if  (remoteHostStatus == ReachableViaWiFi) {NSLog(@"wifi"); self.hasInet-=YES; } 
    else if  (remoteHostStatus == ReachableViaWWAN) {NSLog(@"cell"); self.hasInet-=YES; } 

// if (self.hasInet) { 
//  UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Net avail" message:@"" delegate:self cancelButtonTitle:OK_EN otherButtonTitles:nil, nil]; 
//  [alert show]; 
// } 
} 
+0

handleNetworkThay đổi nhiều lần có thể là không có kết nối wifi nào, Cách kiểm soát –

0

Bạn có thể sử dụng trình bao bọc này trên khả năng truy cập để nhận lệnh gọi lại dựa trên khối bất cứ khi nào có thay đổi về co trạng thái nnectivity.

GitHub: UHBConnectivityManager

Nếu bạn đang sử dụng cocoapod.

pod 'UHBConnectivityManager' 
0

đơn giản nhất của cách:

reach = [Reachability reachabilityWithHostName:@"www.google.com"]; 
reach.reachableBlock = ^(Reachability *reach){ 
    NSLog(@"Reachable"); 
}; 

reach.unreachableBlock = ^(Reachability *reach){ 
    NSLog(@"Unreachable"); 
}; 
[reach startNotifier]; 

Mỗi lần có sự thay đổi trong mạng, các khối tương ứng sẽ được gọi.

0

Đặt khả năng hiển thị làm thuộc tính của bạn thay vì biến cục bộ. Nó sẽ hoạt động.

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