这是我在应用程序中使用可达性的代码:
- (void) handleNetworkChange:(NSNotification *)notice
{
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if(remoteHostStatus == NotReachable) {
UIAlertView *alertnew = [[UIAlertView alloc] initWithTitle:@"No Internet Connection!" message:@"Your device lost internet connection!"
delegate:self
cancelButtonTitle:@"Close"
otherButtonTitles:@"Dismiss", nil];
[alertnew show];
} else if (remoteHostStatus == ReachableViaWWAN) {
//if connected to the Network
UIAlertView *alertre = [[UIAlertView alloc] initWithTitle:@"Internet Connection!" message:@"Your device re-connected to the internet!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok!", nil];
[alertre show];
} else if (remoteHostStatus == ReachableViaWiFi) {
//if connected to Wi-Fi
UIAlertView *alertre = [[UIAlertView alloc] initWithTitle:@"Internet Connection!" message:@"Your device re-connected to the internet!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok!", nil];
[alertre show];
}
}
我想在用户失去互联网以及重新获得互联网时提醒他们。但是我遇到的问题是,当用户使用 iPhone(具有持续的网络连接)并且他们连接到 Wi-Fi 时,他们会在已经连接时收到警报。所以我只想在他们没有互联网连接并连接到 Wi-Fi 时提醒他们。此代码适用于没有数据计划的 iPod 和 iPad,但问题出在 iPhone 上。我可以做些什么来编辑它并让它只显示一个或另一个的警报吗?
我为最后一个警报尝试了这段代码:
else if (remoteHostStatus == ReachableViaWiFi && !(remoteHostStatus == ReachableViaWWAN)) {
//if connected to Wi-Fi but without Network
UIAlertView *alertre = [[UIAlertView alloc] initWithTitle:@"Internet Connection!" message:@"Your device re-connected to the internet!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok!", nil];
[alertre show];
}
但这并没有解决问题...