私はアプリケーションに取り組んでおり、インターネット接続を確認する必要があります。Reachability.h と Reachability.m の 2 つのファイルを使用しています。インターネットが機能していないときにアラートを表示する必要があります。
しかし、オブザーバーがインターネットをチェックすると、アラートビューが複数回表示されます。また、アラートが 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++;
}
}
このコードは委任されており、同様に、インターネットをチェックする必要がある他のクラスでこのコードを使用しています
助けてください