iOS iOS专题资源__APP完整Demo专题iOS Developer

从零开始设计搭建ios App框架(十六)

2016-10-09  本文已影响193人  潇水渔翁

网络状态检测


相信这个功能App都有,使用Reachability几句就代码就可以实现了。
好吧,没什么好说的,直接上代码。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    ........
    
    [self.window makeKeyAndVisible];
    
    //网络检测
    [self networkCheck];
    
    return YES;
}
#pragma mark - 网络检测
- (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:@"无网络连接"];
    }
}
上一篇下一篇

猜你喜欢

热点阅读