在 Github 上使用 Tony Millions 可达性项目。
https://github.com/tonymillion/Reachability
然后在我的应用程序委托中,我只是输入了 applicationdidfinishlaunching
self.reachability = [Reachability reachabilityForInternetConnection];
[self.reachability startNotifier];
然后当状态改变时,这将被调用
#pragma mark - Reachability
- (void)reachabilityChanged:(NSNotification *)note {
NetworkStatus ns = [(Reachability *)[note object] currentReachabilityStatus];
if (ns == NotReachable) {
if (![self.networkAlert isVisible]) {
if ([self networkAlert] == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"\n\nNo Internet Connection" message:@"You require an internet connection to communicate with the server." delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[self setNetworkAlert:alert];
}
[self.networkAlert show];
}
} else {
if ([self networkAlert] != nil) {
[self.networkAlert dismissWithClickedButtonIndex:0 animated:YES];
}
}
}