1亲茅、#import "Reachability.h"http://引頭文件
2、[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];//注冊(cè)通知诈悍,網(wǎng)絡(luò)變動(dòng)時(shí)會(huì)有回調(diào)
NSString *remoteHostName = @"www.apple.com";
self.hostReachability = [Reachability reachabilityWithHostName:remoteHostName];// 測(cè)試服務(wù)器狀態(tài)
[self.hostReachability startNotifier];// 在當(dāng)前程序的運(yùn)行回路中開(kāi)始監(jiān)聽(tīng)網(wǎng)絡(luò)請(qǐng)求可到達(dá)的通知
3、通知函數(shù)
- (void) reachabilityChanged:(NSNotification *)note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
[self stringFromStatus:curReach.currentReachabilityStatus];
}
4靠欢、當(dāng)前網(wǎng)絡(luò)狀況
-(NSString *)stringFromStatus:(NetworkStatus)status
{
NSString *string;
switch (status) {
case NotReachable:
string=@"無(wú)網(wǎng)絡(luò)";
break;
case ReachableViaWiFi:
string=@"wifi環(huán)境";
break;
case ReachableViaWWAN:
string=@"移動(dòng)網(wǎng)絡(luò)";
break;
default:
string=@"未知";
break;
}
return string;
}