我的应用程序正在使用Apple Reachability类。我目前有以下内容:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
_internetReachability = [Reachability reachabilityForInternetConnection];
[_internetReachability startNotifier];
来自AppDelegate:didFinishLaunchingWithOptions
。
可达性通知正常。我测试它是否打开和关闭飞机模式。
但是,如果我在启用飞行模式时启动应用程序,因此在启动时没有连接,则在恢复连接时没有通知(飞机模式已关闭)。
任何想法为什么会这样?该程序如何在恢复连接时收到通知?
reachabilityChanged方法仅检查系统现在是否在线,并发送新通知
Reachability* curReach = [note object];
NetworkStatus internetStatus = [curReach currentReachabilityStatus];
BOOL offline = internetStatus == NotReachable;
NSLog(@"%@ offline = %@", NSStringFromSelector(_cmd), offline ? @"YES" : @"NO");
//only provide notifications if back on-line,
//offline handled after a failed api request
if (!offline)
{
_offline = NO;
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationReachabilityStatusChanged
object:nil
userInfo:@{
kNotificationReachabilityStatusKey : [NSNumber numberWithBool:!_offline]
}];
}