我想讓我的應用程序確定用戶是否有互聯網連接以及它們具有哪種類型的連接。我導入了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");
}
}
我環顧四周,看到很多類似的問題,但解決方案似乎都設置爲我擁有它。
我正在模擬器中工作,如果有關係。任何幫助,將不勝感激。