美文网首页网络监控iOS接下来要研究的知识点
iOS下的实际网络连接检测:RealReachability

iOS下的实际网络连接检测:RealReachability

作者: 辛乐 | 来源:发表于2016-09-19 23:47 被阅读1238次
- 最简便的集成方法当属pod: pod ‘RealReachability’。
- 手动集成:将RealReachability文件夹加入到工程即可。
- 依赖:Xcode5.0+,支持ARC, iOS6+.项目需要引入SystemConfiguration.framework.

//打开检测
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    1.[GLobalRealReachability startNotifier];
    return YES;
}

//在具体的页面添加观察者(实时监测网络的变化)
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    2.[[NSNotificationCenter defaultCenter] addObserver:self
                                               selector:@selector(networkChanged:)
                                                   name:kRealReachabilityChangedNotification
                                                 object:nil];
    
    //当前网络连接状态 (单例调用简单)
    3.ReachabilityStatus status = [GLobalRealReachability currentReachabilityStatus];
    
    NSLog(@"Initial reachability status:%@",@(status));
    
    if (status == RealStatusNotReachable)
    {
        self.flagLabel.text = @"Network unreachable!";
    }
    
    if (status == RealStatusViaWiFi)
    {
        self.flagLabel.text = @"Network wifi! Free!";
    }
    
    if (status == RealStatusViaWWAN)
    {
        self.flagLabel.text = @"Network WWAN! In charge!";
    }
}

//观察者实时检测方法
- (void)networkChanged:(NSNotification *)notification
{
    RealReachability *reachability = (RealReachability *)notification.object;
    ReachabilityStatus status = [reachability currentReachabilityStatus];
    ReachabilityStatus previousStatus = [reachability previousReachabilityStatus];
    NSLog(@"networkChanged, currentStatus:%@, previousStatus:%@", @(status), @(previousStatus));
    
    if (status == RealStatusNotReachable)
    {
        self.flagLabel.text = @"Network unreachable!";
    }
    
    if (status == RealStatusViaWiFi)
    {
        self.flagLabel.text = @"Network wifi! Free!";
    }
    
    if (status == RealStatusViaWWAN)
    {
        self.flagLabel.text = @"Network WWAN! In charge!";
    }
    
    WWANAccessType accessType = [GLobalRealReachability currentWWANtype];
    
    if (status == RealStatusViaWWAN)
    {
        if (accessType == WWANType2G)
        {
            self.flagLabel.text = @"RealReachabilityStatus2G";
        }
        else if (accessType == WWANType3G)
        {
            self.flagLabel.text = @"RealReachabilityStatus3G";
        }
        else if (accessType == WWANType4G)
        {
            self.flagLabel.text = @"RealReachabilityStatus4G";
        }
        else
        {
            self.flagLabel.text = @"Unknown RealReachability WWAN Status, might be iOS6";
        }
    }
    
    
}

相关文章

网友评论

  • b2efe7751b24:升级到xcode9就报这个错误了怎么办?
    Undefined symbols for architecture x86_64:
    "_OBJC_CLASS_$_RealReachability", referenced from:
    迟明子:我的也是不能用了,PingFoundation.h 里面报了几个错.....现在不能用了.
    b2efe7751b24:@庄周_ce63 没有,只能删了
    庄周_ce63:我也是,你解决了么
  • 远山风渡月:请教一下,有网络切换到无网络,判断正确,为什么从无网络切换到有网络,通知没有被响应,手动获取状态值也是返回无网络?是我的使用哪里出问题了吗?
    远山风渡月:@ArrQing 谢谢回复哈
    辛乐:@ArrQing 好的,谢了老铁~不晚
    ArrQing:嗯,不知道这个时候回答你算晚不,监听 也要放到 didFinishLaunchingWithOptions 里面。

本文标题:iOS下的实际网络连接检测:RealReachability

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