網(wǎng)絡狀態(tài)檢測
相信這個功能App都有,使用Reachability幾句就代碼就可以實現(xiàn)了。
好吧,沒什么好說的,直接上代碼。
- (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)絡檢測
[self networkCheck];
return YES;
}
#pragma mark - 網(wǎng)絡檢測
- (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ǎng)絡連接"];
}
}