查看控制器以确定可达性,如果应用程序连接到互联网,我在这里有以下代码:
//Check for internet
internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];
// Internet is reachable
internetReachableFoo.reachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Yayyy, we have the interwebs!");
});
};
// Internet is not reachable
internetReachableFoo.unreachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Someone broke the internet :(");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Important" message: @"You are not connected to the internet. Please try again." delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show];
});
};
[internetReachableFoo startNotifier];
}
这一切都很完美,但是当我没有互联网时,警报视图会弹出,然后当我再次上网时,整个脚本会再次运行,并且当它检测到互联网正在连接到它实际返回时有一个小的延迟google.com 并因此在说它最终连接之前检查并声称互联网无法访问。要点是,由于它声称两次未连接互联网,因此会两次显示 UIAlertView。我有什么办法只让警报视图显示一次。我试过移动这个脚本,但我不太熟悉我可以在警报视图上做的更多事情,以使其不再被调用。任何帮助表示赞赏。