網(wǎng)絡(luò)狀態(tài)檢測(cè)
相信這個(gè)功能App都有,使用Reachability幾句就代碼就可以實(shí)現(xiàn)了。
好吧,沒(méi)什么好說(shuō)的,直接上代碼。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
........
[self.window makeKeyAndVisible];
//網(wǎng)絡(luò)檢測(cè)
[self networkCheck];
return YES;
}
#pragma mark - 網(wǎng)絡(luò)檢測(cè)
- (void)networkCheck
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
self.hostReach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
[self.hostReach startNotifier];
}
- (void)reachabilityChanged:(NSNotification *)note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus status = [curReach currentReachabilityStatus];
if (status == NotReachable)
{
[self.window.rootViewController showTitle:@"提示" msg:@"無(wú)網(wǎng)絡(luò)連接"];
}
}