在我的应用中,我检查了我网站的当前连接状态以及应用启动时的网络状态:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification object:nil];
internetReach = [[Reachability reachabilityForInternetConnection] retain];
[internetReach startNotifier];
hostReach = [[Reachability reachabilityWithHostName:@"www.google.de"]
retain];
[hostReach startNotifier];
[self updateInterfaceWithReachability:internetReach];
[self updateInterfaceWithReachability:hostReach];
//....
-(void)reachabilityChanged: (NSNotification* )note{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
[self updateInterfaceWithReachability: curReach];
}
在我的updateInterfaceWithReachability
中,如果无法建立与网站的连接,我想显示提醒。
我的问题是警报显示两次,所以第一次显示,当我关闭它时会显示秒数:
if(curReach == hostReach){
NetworkStatus netStatus = [curReach currentReachabilityStatus];
if(netStatus != NotReachable){
statusLabel.text = @"connected";
[self setStatusColorGreen];
}else{
if(![internetReach connectionRequired]){
statusLabel.text = @"not connected";
compose.enabled = NO;
[self doSMSFAlert];
[self setStatusColorRed];
}
}
}
有什么想法吗?
编辑:我现在注意到我有一个工作主机,警报也显示但不应该显示。但状态为“已连接”答案 0 :(得分:3)
因为您正在进行两次可达性通知。你的第一个是一般的互联网。第二个是针对特定主机的。你为什么不只做主机可达性?这样你只会得到一个警报。
答案 1 :(得分:1)
您正在回复通知事件。事件可以在状态更改时多次传递,您可以知道自己是否已经响应或正在响应类似的状态更改。一些选项是存储布尔值,或者在尝试显示另一个之前检查警报视图当前是否已启动。