• Rechability的简单使用


    AppDelegate.m

    #import "AppDelegate.h"
    #import "Reachability.h"
    
    @interface AppDelegate ()
    
    @property (nonatomic, strong) Reachability *reachability;
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // 监听网络状态的改变
        [self networkChange];
        
        return YES;
    }
    
    // 监听网络变化
    - (void)networkChange
    {
        // 开启网络状况的监听通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
        // 监听的链接
        self.reachability = [Reachability reachabilityWithHostName:@"www.baidu.com"];
        // 开始监听, 会启动一个Runloop
        [self.reachability startNotifier];
    }
    
    // 网络链接改变时会调用的方法
    - (void)reachabilityChanged:(NSNotification *)note
    {
        // 通过通知对象获取被监听的Reachability对象
        Reachability *currReach = [note object];
        NSAssert([currReach isKindOfClass:[Reachability class]], @"非Reachability类");
        
        //对连接改变做出响应处理动作
        NetworkStatus status = [currReach currentReachabilityStatus];
        
        //如果没有连接到网络就提醒实况
        if(status == NotReachable) {
            NSLog(@"AppDelegate - 网络连接异常");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"networkChangeNotification" object:nil userInfo:@{@"networkStatusKey" : @(NO)}];
        } else {
            NSLog(@"AppDelegate - 网络连接正常");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"networkChangeNotification" object:nil userInfo:@{@"networkStatusKey" : @(YES)}];
        }
    }
    
    @end

    ViewController.m

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // 监听网络状态
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkChange:) name:@"networkChangeNotification" object:nil];
    }
    
    - (void)dealloc
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    - (void)networkChange:(NSNotification *)notification
    {
        NSString *status = notification.userInfo[@"networkStatusKey"];
        
        if (status.boolValue) {         // 断网切换为正常状态
            NSLog(@"断网切换为正常状态");
        } else {                        // 正常切换为断网状态
            NSLog(@"正常切换为断网状态");
        }
    }
    
    @end

    Demo:

    https://github.com/RinpeChen/RechabilityDemoByRinpe

  • 相关阅读:
    linux下使用hash_map及STL总结
    编写Linux系统下Daemon程序的方法步骤
    c语言版网络爬虫spiderq
    利用unordered_map代替hash_map My Study
    Mike Wallace of '60 Minutes' to retire
    让Redis使用TCMalloc,实现高性能NOSql服务器
    守护进程的单实例实现_非宁静无以致远_百度空间
    sscanf,sscanf_s及其相关用法 小 楼 一 夜 听 春 雨 博客园
    实现个hash_map容器类玩玩 苍梧 博客园
    [Linux初级]Linux下动态库的生成及链接方法
  • 原文地址:https://www.cnblogs.com/Rinpe/p/5048843.html
Copyright © 2020-2023  润新知