Reachability的示例程序:
https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
不變的:
// 監(jiān)聽(tīng)網(wǎng)絡(luò)狀態(tài)改變的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil];
// 創(chuàng)建Reachability
self.conn = [Reachability reachabilityForInternetConnection];
// 開(kāi)始監(jiān)控網(wǎng)絡(luò)(一旦網(wǎng)絡(luò)狀態(tài)發(fā)生改變, 就會(huì)發(fā)出通知kReachabilityChangedNotification)
[self.conn startNotifier];
改變的
- (NSString *)networkStateChange{
NSInteger currentStatu = [[Reachability reachabilityForInternetConnection] currentReachabilityStatus];
switch (currentStatu) {
case NotReachable:
return @"無(wú)網(wǎng)絡(luò)";
break;
case ReachableViaWiFi:
return @"Wifi";
break;
case ReachableViaWWAN:
return @"G網(wǎng)";
break;
default:
return @"";
break;
}
}