Git Product home page Git Product logo

realreachability's Introduction

RealReachability

Version License Platform Platform

We need to observe the REAL reachability of network for iOS. That's what RealReachability do.

Why RealReachability?

As we know, we already have reachability framework for us to choose. Such as the famous repository Reachability.

BUT we really need a tool for us to get the reachability, not the local connection!

Apple doc tells us something about SCNetworkReachability API: "Note that reachability does not guarantee that the data packet will actually be received by the host."

The called "reachability" we already know can only tell us the local connection status.These tools currently we know are all supported by the SCNetworkReachability API.

Now RealReachability can do this for you~

We introduce ping module for us to check the real network status, together with SCNetworkReachability API. And we use FSM(finite state machine) to control all of the network status to confirm that only status change will be sent to application.

Enjoy it!

Quick Start With Cocoapods

CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like RealReachability in your projects. You can install it with the following command:

$ gem install cocoapods

Podfile

To integrate RealReachability into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

pod 'RealReachability'

Then, run the following command:

$ pod install

Installation with Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

RealReachability in your Cartfile:

github "dustturtle/RealReachability"

Manual Start

If you'd rather do everything by hand, just add the folder "RealReachability" to your project, then all of the files will be added to your project.

Dependencies

  • Xcode 5.0+ for ARC support, automatic synthesis and compatibility libraries. iOS 6.0+.
  • The SystemConfiguration Framework should be added to your project.

Usage

Start to notify(we suggest you to start notify in didFinishLaunchingWithOptions):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [GLobalRealReachability startNotifier];
    return YES;
}

Add Observer(anywhere you like):

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(networkChanged:)
                                             name:kRealReachabilityChangedNotification
                                           object:nil];

Observer method like below:

- (void)networkChanged:(NSNotification *)notification
{
    RealReachability *reachability = (RealReachability *)notification.object;
    ReachabilityStatus status = [reachability currentReachabilityStatus];
    NSLog(@"currentStatus:%@",@(status));
}

Trigger realtime Reachability like below:

[GLobalRealReachability reachabilityWithBlock:^(ReachabilityStatus status) {
        switch (status)
        {
            case RealStatusNotReachable:
            {
            //  case NotReachable handler
                break;
            }
                
            case RealStatusViaWiFi:
            {
            //  case WiFi handler
                break;
            }
                
            case RealStatusViaWWAN:
            {
            //  case WWAN handler
                break;
            }
                
            default:
                break;
        }
    }];

Query currentStatus

ReachabilityStatus status = [reachability currentReachabilityStatus];

Once the reachabilityWithBlock was called, the "currentReachabilityStatus" will be refreshed synchronously.

Set your own host for Ping (optional)

Note that now we introduced the new feature "doublecheck" to make the status more reliable in 1.2.0!

Please make sure the host you set here is available for pinging. Large, stable website suggested.
This step is optional. If you do not set this, our default host is: www.apple.com.
You may set your own host any time you like. Codes just like below:

GLobalRealReachability.hostForPing = @"www.apple.com";
GLobalRealReachability.hostForCheck = @"www.youOwnHostExample.com";

We suggest you use two hosts: one your own(if you have one available for pinging), one public; Just like the example below.

For more details about the "doublecheck" feature, you can go deep into the codes.

Get current WWAN type (optional)

 WWANAccessType accessType = [GLobalRealReachability currentWWANtype];

Current WWAN type might be used to improve your app's user experience(e.g, set different network request timeout interval for different WWAN type).

Check the VPN status of your network

- (BOOL)isVPNOn;

With the help of this method, we have improved our reachability check logic when using VPN.

More:

We can also use PingHelper or LocalConnection alone to make a ping action or just observe the local connection.
Pod usage like blow (we have two pod subspecs):

pod 'RealReachability/Ping'
pod 'RealReachability/Connection'

This is the only API we need to invoke about Ping:

- (void)pingWithBlock:(void (^)(BOOL isSuccess))completion;

More about the ping usage, please see the PingHelper.h or codes in the demo project.

LocalConnection module is very similar with Reachability.
More about its usage, please see the LocalConnection.h or codes in the demo project.

Demo

We already put the demo project in the repository.

License

RealReachability is released under the MIT license. See LICENSE for details.

And finally...

Please use and improve! Patches accepted, or create an issue.

I'd love it if you could send me a note as to which app you're using it with! Thank you!

支持我

老司机技术周报出品的《WWDC 内参》系列,一直是 iOS 开发的精品阅读,几乎涵盖了每年需要了解的所有 iOS 新技术。尤其是今年《WWDC21 内参》的质量比去年有了比较大的提升, 作为作者之一,在这里安利给大家: https://xiaozhuanlan.com/wwdc21?rel=4203097925。目前活动价五折销售,抓紧入手啦。

realreachability's People

Contributors

bryant1410 avatar daemonson avatar dustturtle avatar hankjacobs avatar leomehlig avatar lvyzhu avatar prayagverma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

realreachability's Issues

Benefits over current approach

Hey,

Just out of curiosity, what are the benefits of using this over Reachability or SCNetworkReachability.

My impression is that this would allow for faster feedbacks into the connection status by using pinging, is that it?

Thanks!

检测不了的网络多次关闭和打开

RealReachability *t=[[RealReachability alloc]init];
t.hostForPing=@"www.baidu.com";
[t startNotifier];
[t reachabilityWithBlock:^(ReachabilityStatus status) {

    NSLog(@"status %ld",status);
}];

我利用上述代码,然后不停打开或者关闭网络,都没有打印出网络状态,这个如何解决?

关于打印日志的问题

个人觉得正常情况下打印日志是为了调试和查看错误状态信息,RealReachability库的使用者只关心库是否正常响应,虽然生产环境去除了日志,但是在开发环境,对于多余正常的日志信息我们也不关心,反而会影响我们查看其它错误日志信息,看到那么多无用的日志,真的很烦。所以我建议作者做2个调整:
1、添加宏定义并用累似RRLog来替换RealReachability中的NSLog

ifdef DEBUG

define RRLog(...) NSLog(VA_ARGS)

else

define RRLog(...)

endif

2、提供一个日志开关,用于控制打印日志(默认关掉)。

发现一个重复发送通知问题

你好,感谢提供这么优秀的开源代码。在使用过程中,我发现在切换网络的时候有时(概率并不高)会接收到两次通知,而且第二次通知事件中获取到的网络连接状态是错误的。这种情况只出现在由无网络切换到有网络的时候,也就是说,从无网络切换到有网络时,先收到了一个网络变化通知,在这个通知事件中获取到的网络状态是“连接状态”,然后又收到了另一个网络变化通知,在这个通知事件中获取到的网络状态是“不可连接状态”,这会导致程序出现异常。我跟了下代码,发现用户在收起系统快捷操作面板(就是从屏幕底部滑出的那个面板)的时候会触发reachabilityWithBlock事件,在localConnectionChanged事件后面也有触发reachabilityWithBlock事件,有可能是reachabilityWithBlock中发出了重复的通知。不知道这是不是一个bug,还是我使用的方法有问题。

从wifi切换到4G状态不对

运行demo,系统是9.3.1的iphone6,从底部拉出系统菜单,关闭wifi按钮,从wifi切换到4g,会出现提示RealReachabilityStatus4G,接着再次提示为Network unreachable!从底部拉出系统菜单,打开wifi按钮,从4G切换到wifi也会出现Network wifi! Free,再次提示为Network unreachable!原因是第一次正常,第二次就都是notreachable,反复较快切换,这种情况出现的频率较为高;尤其是从wifi切换到4g,状态出现错误的概率很大(现象同前面描述);

Conflicts with Reachability

Hi,
I have always wanted this. To be notified about true connections.

After finding this repo, I tried to add it to one of my existing projects. The project has many many dependencies which in turn have their own dependencies and from there it goes a downward spiral ... anyway, somewhere along the way, one of these projects has Reachability as its dependency. Now, I can't remove the main dependency (not for the time being at least).

When I added RealReachability to my project, i have faced a conflict between RealReachability and Reachability on the NetworkStatus enum cases:

typedef NS_ENUM(NSInteger, NetworkStatus) {
    // Apple NetworkStatus Compatible Names.
    NotReachable = 0,
    ReachableViaWiFi = 2,
    ReachableViaWWAN = 1
};

Is it possible to change the enum cases for the unlucky guys like me who want to keep Reachability and RealReachability coexist in one project?

首次进入时候 (void)networkChanged监听错误

问题描述:在某个页面注册监听,如果当前网络是3G并且开WIFI,那么会必然进入networkChanged,其实网络本身并没有发生变化,不应当进入networkChanged回调函数

网络侦测不正常

1:延迟问题,我在didFinishLaunchingWithOptions [GLobalRealReachability startNotifier] 进入app没有及时获取网络信息(wifi环境)

2:网络明明正常链接 过了一会儿又变成RealStatusNotReachable

不知道是ios7还是因为手机是越狱的情况,判断不可用啊

如题,就是我有个4s,越狱的,ios7,有做一些判断网络环境的操作,然后经常就是连着wifi,开始调试,点击,点击某个按钮进行网络请求,然后就判断成为了不是wifi个2 G 3 G 4G 的那个选项,另外2个就是一个没联网一个未知嘛。我这是连着公司wifi的,所以不知道是什么情况,求大神指教?(这个判断网不对我就只有重新commen+r)

网络监控极度不稳定

我们使用这个库监控网络状态后,用户大量反馈没有网络。但用户都是有网络的。要么是 wifi,要么是4G 状态下面的。造成用户不能使用其他功能了。

断网后重新连接后的问题

我在 appdelegate.m 中添加的检测网络状况变化的通知
联网状态下 当第一次断网后 提示 网络断开 正常
再次联网后 刷新页面 正常 但是会增加一个 网络断开 的提示

demo 中 联网 断网 联网 这三种状态改变 后 页面上的label中 显示顺序 network wifi free - network unreachable - network wifi free - network unreachable

这是为什么呢

在Swift下怎么使用

我用bridge在swift工程下使用,但总是用不起来,这个支持Swift模式下使用吗?还希望能够给个Swift下使用的例子。

100%丢包貌似不会当做无网络

我用Network Link Conditioner开启100%丢包, 貌似不会被当作无网络处理, 按道理这个时候应该是ping不通然后会触发无网络的通知才对.

PingHelper问题

pingWithBlock:(void (^)(BOOL isSuccess))completion方法的completion中调用pingWithBlock:,第二次的block参数会在pingTimeOut之前被 [self.completionBlocks removeAllObjects] 删除,导致第二个completion不能被调用。

更新之后我这发现crash

RealReachability的init方法里
_hostForPing = kDefaultHost;这样写,没走- (void)setHostForPing:(NSString *)hostForPing这个方法,导致GPingHelper.host设置不成功,会在

  • (instancetype)initWithHostName:(NSString *)hostName {
    NSParameterAssert(hostName != nil);
    self = [super init];
    if (self != nil) {
    self->_hostName = [hostName copy];
    self->_identifier = (uint16_t) arc4random();
    }
    return self;
    }
    这个方法里崩溃。
    改为self.hostForPing = kDefaultHost;没问题

多次调用的问题

  • (void)networkChanged:(NSNotification *)notification
    {

}
这个方法会调用多次。切换网络的时候

移除NSLog

快要把Ping的log移掉吗,以一个第三方库来说,这种log算是侵入式的了。

你好,我想问一下 这个类库是否兼容IPv6

我现在项目中官方的那个网络监控 是3.5版的,不兼容IPv6,也没在官网找到最新版本的下载地址,直搜索到你上传到GitHub上这个版本,看时间时最新的,而且还是你封装好的,所有想知道是否支持,项目中其他的兼容设置我都已经改好了。6月份还有新版本上传app store,望看到后回复,谢谢!~

一直显示无网络

当前存在网络连接 ReachabilityStatus status = [GLobalRealReachability currentReachabilityStatus];
status却变成RealStatusNotReachable,在3G4G和WiFi下都是这样

可否增加对ip+port的检测支持?

你好
我在看了源码后,发现PingFoundation的start方法中
self.host是用CFHostCreateWithName方法生成的
我自己又加了用CFHostCreateWithAddress方法生成self.host
但是发现加了端口号以后,始终都是返回无连接状态
请问这个是为什么呢?

网络监测情况不是很准

1.我在正常WiFi和3G之间切换还算正常,当我通过代理链接的时候,网络的实时监测情况就不准,有两种情况:
a.我代理填了一个假的代理(连接了WiFi但不能上网的情况)之后再切换真的代理(连接了WiFi且能上网的情况),就会经常出错,返回的链接基本上都是WiFi链接正常;
b.我代理填了一个假的代理(连接了WiFi但不能上网的情况)之后再切换真的代理(连接了WiFi且能上网的情况),然后在真代理和3G之间切换,基本上也会经常出错;

Crashes in multi threaded app usage.

I get the same crash dump a lot of times. Every time, the thread that this is being done is some background thread.
I noticed you are starting the pingFoundation in main thread. Do we also need to stop it in main thread?

The below is the dump of the crash generated by Hockey

Thread 11 Crashed:
0x0000000181e459cc CFRelease + 1128
0x00000001005f5ec0 -PingFoundation stopDataTransfer
0x00000001005f5efc -PingFoundation stop
0x00000001005f62c0 -PingHelper pingWithBlock:
0x00000001005f77a0 -RealReachability reachabilityWithBlock:
0x00000001005f7e40 __41-[RealReachability autoCheckReachability]_block_invoke (RealReachability.m:253)
0x00000001819b9630 _dispatch_call_block_and_release + 20
0x00000001819b95f0 _dispatch_client_callout + 12
0x00000001819c4e7c _dispatch_after_timer_callback + 88
0x00000001819b95f0 _dispatch_client_callout + 12
0x00000001819d0204 _dispatch_source_latch_and_call + 2552
0x00000001819bbae4 _dispatch_source_invoke + 804
0x00000001819c7504 _dispatch_root_queue_drain + 724
0x00000001819c7224 _dispatch_worker_thread3 + 108
0x0000000181bcd470 _pthread_wqthread + 1088
0x0000000181bcd020 start_wqthread + 0

Does not work

"warning! reachState uncertain! state unmatched, treat as unreachable temporary"
And on Simulator and on device.

在WiFi情况下,网络波动,从网络不可达到有网络状态回调很慢

网络波动(并非切换网络方式),出现网络不可达,但是这是这一点不可达,未来的非常快的一个时间点恢复网络了,触发通知回调很慢(对于这一点的验证:我在网络不可达以后立马通过提供的block获取但网络状态是可达的)
代码如下:

import "RRController.h"

import "RealReachability.h"

@interface RRController ()

@Property (weak, nonatomic) IBOutlet UILabel statusLabel; /*< 网络状态视图 */

  • (IBAction)clickBntForRR:(id)sender;

@EnD

@implementation RRController

pragma mark - life cycle

  • (void)viewDidLoad
    {
    [super viewDidLoad];

    [self setupUIForRR];
    }

  • (void)viewWillAppear:(BOOL)animated
    {
    [self refreshNetworkStatus];
    }

  • (void)dealloc
    {
    BKLog(@"");

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    }

pragma mark - event response

  • (IBAction)clickBntForRR:(id)sender
    {
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *detailController = [main instantiateViewControllerWithIdentifier:@"RRDetailController"];

    if (detailController)
    {
    [self.navigationController pushViewController:detailController animated:YES];
    }
    }

pragma mark - callback

  • (void)applicationDidBecomeActive
    {
    [self refreshNetworkStatus];
    }

  • (void)networkChanged:(NSNotification *)notification
    {
    RealReachability *reachability = (RealReachability *)notification.object;
    ReachabilityStatus status = [reachability currentReachabilityStatus];

    BKLog(@"status = %zd", status);

    [self refreshStatusLabel:status];
    }

pragma mark - private method

  • (void)setupUIForRR
    {
    // notifications
    // 监听网络状态
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self
    selector:@selector(networkChanged:)
    name:kRealReachabilityChangedNotification
    object:nil];
    // 监听程序获取焦点
    [defaultCenter addObserver:self
    selector:@selector(applicationDidBecomeActive)
    name:UIApplicationDidBecomeActiveNotification
    object:nil];
    }

  • (void)refreshNetworkStatus
    {
    [[RealReachability sharedInstance] reachabilityWithBlock:^(ReachabilityStatus status) {
    BKLog(@"status = %zd", status);

    [self refreshStatusLabel:status];
    

    }];
    }

  • (void)refreshStatusLabel:(ReachabilityStatus)status
    {
    switch (status) {
    case RealStatusNotReachable:
    {
    [_statusLabel setBackgroundColor:[UIColor yellowColor]];
    [_statusLabel setText:@"无网络!"];
    break;
    }
    case RealStatusViaWWAN:
    {
    [_statusLabel setBackgroundColor:[UIColor greenColor]];
    [_statusLabel setText:@"手机网络!"];
    break;
    }
    case RealStatusViaWiFi:
    {
    [_statusLabel setBackgroundColor:[UIColor greenColor]];
    [_statusLabel setText:@"WiFi网络!"];
    break;
    }
    default:
    break;
    }
    }

@EnD
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.