我正在开发一个应用程序,我必须检查互联网连接。我正在使用两个文件 Reachability.h 和 Reachability.m。现在我必须在互联网不工作时显示警报。
但是,当我的观察者检查互联网时,它会多次显示警报视图。有时它会显示更多 9-10 次警报。我的代码在这里:
在 .h 文件中
Reachability* internetReachable;
Reachability* hostReachable;
NetworkStatus internetStatus;
在 .m 文件中
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[hostReachable startNotifier];
- (void) checkNetworkStatus:(NSNotification *)notice
{
internetStatus = [internetReachable currentReachabilityStatus];
if (internetStatus != NotReachable){
NSLog(@"internet is on");
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Server not connected or down!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
internetCount++;
}
}
此代码在委托中,同样我在其他必须检查互联网的课程中使用此代码
请帮助