美文网首页iOS iOS专题资源__APP完整Demo专题iOS Developer
从零开始设计搭建ios App框架(十六)

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

作者: 潇水渔翁 | 来源:发表于2016-10-09 15:17 被阅读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:@"无网络连接"];
    }
}

相关文章

网友评论

    本文标题:从零开始设计搭建ios App框架(十六)

    本文链接:https://www.haomeiwen.com/subject/nznpyttx.html