2013-07-29 22 views
0

我想讓我的應用程序確定用戶是否有互聯網連接以及它們具有哪種類型的連接。我導入了SystemConnection框架和Reachability .h和.m文件。objective-c可達性類

在我viewController.h我有以下幾點:

#import "Reachability.h" 

Reachability* reachability; 
在checkConnectivity

和vc.m

//notification for network status change 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNetworkChange:) name:kReachabilityChangedNotification object:nil]; 

[[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification object:nil]; 

[reachability startNotifier]; 


//check connectivity 
[self checkConnectivity]; 

- (void) checkConnectivity { 

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus]; 

    if(remoteHostStatus == NotReachable) { 
     NSLog(@"no connection"); 
    } else if (remoteHostStatus == ReachableViaWiFi) { 
     NSLog(@"wifi"); 
    } else if (remoteHostStatus == ReachableViaWWAN) { 
     NSLog(@"cell"); 
    } 

} 

能正常工作的啓動。我記錄了進度,並按預期返回:

2013-07-29 09:35:17.084 OAI_Project_Template[6095:c07] not connected - network change 
2013-07-29 09:35:17.093 OAI_Project_Template[6095:c07] wifi- check connectity 

但是,如果我打開或關閉我的無線連接handleNetworkChange永遠不會被調用。

- (void) handleNetworkChange : (NSNotification*) notification { 

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus]; 

    if(remoteHostStatus == NotReachable) { 
    isConnected = NO; 
    NSLog(@"not connected - network change"); 
    } else if (remoteHostStatus == ReachableViaWiFi) { 
    NSLog(@"wifi - network change"); 
    } else if (remoteHostStatus == ReachableViaWWAN) { 
    NSLog(@"cell"); 
    } 
} 

我環顧四周,看到很多類似的問題,但解決方案似乎都設置爲我擁有它。

我正在模擬器中工作,如果有關係。任何幫助,將不勝感激。

回答

0

這是我必須檢測網絡。基本上,我需要的只是檢測應用程序是否可以訪問互聯網上的服務器,一旦發佈通知,請檢查訪問權限並根據訪問權限進行處理。確保只有在應用程序處於前臺時才註冊可達性。

// Called from applicationDidBecomeActive 

- (void) startMonitoring 
{ 
    //Register for change in reachability 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; 

    // Setup a target server to detect if a host on the internet can be accessed . For example www.apple.com. Defined as instance variable 
     hostReach = [Reachability reachabilityWithHostName: @"www.apple.com"]; 
     [hostReach startNotifier]; 
} 

- (void)reachabilityChanged:(NSNotification *)note 
{ 
    //NSLog(@"%s %@", __FUNCTION__, note); 
    Reachability* curReach = [note object]; 
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]); 

    [self updateInterfaceWithReachability: curReach]; 

} 
- (void)updateInterfaceWithReachability: (Reachability*) curReach 

{ 


// Check if the host site is online 
NetworkStatus hostStatus = [hostReach currentReachabilityStatus]; 
switch (hostStatus) 
{ 
    case NotReachable: 
    { 
     NSLog(@"%s No access - ", __FUNCTION__); 

     break; 
    } 
    case ReachableViaWiFi: 
    { 
     // Check for LAN switch 
     NSLog(@"%s WIFI Available - ", __FUNCTION__); 
     break; 
    } 
    case ReachableViaWWAN: 
    { 
     // Disable LAN switch 
     NSLog(@"%s WIFI NOT Available ", __FUNCTION__); 

     break; 
    } 
} 
}