2014-03-05 67 views
0

我使用的是reachability類,其中我必須調用其中的方法稱爲「startNotifier」,後者又調用回調函數。我正在使用創建一個包含reachability.h/.m和兩個其他文件與下面的代碼:在庫中調用回調

wifi.h

#import <Foundation/Foundation.h> 
#import "Reachability.h" 

@protocol MyDelegate 
-(void)currentInternetStatus:(BOOL)isInternetAvailable; 
@end 

@interface wifi : NSObject 
{ 
    Reachability *internetReachable; 
    Reachability *hostReachable; 
    id <MyDelegate>delegate ; 
} 
#pragma mark Booleans 
@property BOOL hostActive; 
@property BOOL internetActive; 
@property (nonatomic,retain)id <MyDelegate>delegate ; 

#pragma mark Functions 
-(void)internetAvailability; 

@end 

wifi.m

#import "wifi.h" 

@implementation wifi 

@synthesize hostActive,internetActive,delegate; 

#pragma mark Methods for checking Network Connection 

/** Method for checking Network Connection*/ 
-(void)internetAvailability 
{ 
[[NSNotificationCenter defaultCenter]removeObserver:self name:kReachabilityChangedNotification object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil]; 
internetReachable = [[Reachability alloc]init]; 
//Reachability *internetReachable = [Reachability reachabilityForInternetConnection]; 
internetReachable = [Reachability reachabilityForLocalWiFi]; 
[internetReachable startNotifier]; 
} 


/** Notification called to check networks presence*/ 
- (void) checkNetworkStatus:(NSNotification *)notice 
{ 
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; 
    switch (internetStatus) 

    { 
     case NotReachable: 
     { 
      NSLog(@"The internet is down."); 

      self.internetActive = NO; 
      [self.delegate currentInternetStatus:NO]; 
      break; 

     } 
     case ReachableViaWiFi: 
     { 
      NSLog(@"The internet is working via WIFI."); 
      self.internetActive = YES; 
      [self.delegate currentInternetStatus:NO]; 
      break; 

     } 
     case ReachableViaWWAN: 
     { 
      NSLog(@"The internet is working via WWAN."); 
      self.internetActive = YES; 
      [self.delegate currentInternetStatus:NO]; 
      break; 

     } 
    } 

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus]; 
    switch (hostStatus) 

    { 
     case NotReachable: 
     { 
      NSLog(@"A gateway to the host server is down."); 
      self.hostActive = NO; 

      break; 

     } 
     case ReachableViaWiFi: 
     { 
      NSLog(@"A gateway to the host server is working via WIFI."); 
      self.hostActive = YES; 

      break; 

     } 
     case ReachableViaWWAN: 
     { 
      NSLog(@"A gateway to the host server is working via WWAN."); 
      self.hostActive = YES; 

      break; 

     } 
    } 

} 
@end 
當我寫的appdelegate它的工作原理相同的代碼,但

當我寫相同的(.H /.m) 它不在庫中。

這是我如何使用它:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    wifi *obj = [[wifi alloc]init]; 
    obj.delegate = self; 
    [obj internetAvailability]; 

    return YES; 
} 

- (void)currentInternetStatus:(BOOL)isInternetAvailable 
{ 
    NSLog(@"net status %d", isInternetAvailable); 
} 
+0

描述它是如何工作的。顯示你如何使用'wifi'類。你爲什麼保留代表? – Wain

+0

我已經在上面添加了代碼。 – user2798258

+0

那麼,它怎麼不起作用?它是否編譯?如果是,我猜它運行,所以問題是你沒有收到你的'currentInternetStatus:'回調:是嗎? – Romain

回答

0

您建立wifi *obj = [[wifi alloc]init];,但從來沒有保留它。所以,application:didFinishLaunchingWithOptions:完成後,實例被破壞。

因爲檢查網絡可用性的過程是異步的,所以不會發送通知。

您需要保留obj實例。例如,將其存儲在AppDelegate的「強」屬性中。

+0

我試過你說的它仍然沒有wrk – user2798258

+0

是'checkNetworkStatus:'有沒有叫?你如何測試? – Wain

+0

對不起,它正在工作。我錯過了別的東西。非常感謝你救了我:) – user2798258