從零開始設計搭建ios App框架(十六)

分類:技術 時間:2016-10-25

網絡狀態檢測

相信這個功能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:@quot;www.baidu.comquot;];
    [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:@quot;提示quot; msg:@quot;無網絡連接quot;];
    }
}

Tags: iOS開發

文章來源:http://www.jianshu.com/p/abd871d7ecc3


ads
ads

相關文章
ads

相關文章

ad