currentReachabilityStatus阻塞

来源:互联网 发布:pandas 数据分析 实例 编辑:程序博客网 时间:2024/06/02 04:55
问题现象:连接到一个无法连接到外网的局域网或者时延很大的网络,我们的IOS应用一直处于加载界面,然后过一段时间后闪退。
问题分析:因为在我们应用的第一个界面的onEnter中有针对当前网络的判断,其中使用到了Reachability的 currentReachabilityStatu来判断当前网络的连接情况。当网络情况很差或者是局域网无外网连接的时候,Reachability 会出现阻塞,从而阻塞了应用的启动(触发了failedto launch in time),而导致我们的应用被系统强行kill.
解决方法:使用异步线程调用Reachability 或者使用通知机制来判断网络的连接情况,具体可参考:
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];//Change the host name here to change the server your monitoring    remoteHostLabel.text = [NSString stringWithFormat: @"Remote Host: %@", @"www.apple.com"];    hostReach = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];    [hostReach startNotifier];

- (void) reachabilityChanged: (NSNotification* )note{    Reachability* curReach = [note object];    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);    [self updateInterfaceWithReachability: curReach];}


0 0