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

网络状态检测


相信这个功能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:@"无网络连接"];
    }
}

你可能感兴趣的:(从零开始设计搭建ios App框架(十六))