我的应用中有一个需要互联网连接才能运行的控制器。我知道如果设备在应用程序开始之前有或没有互联网连接,我必须做什么。虽然我一直坚持用户必须启用互联网连接到应用程序的问题。我有一个方法,检查是否有互联网连接,如果没有,它显示一个警报视图。我希望保留到此视图控制器,直到启用Internet。这是我的功能的代码不起作用。欢迎任何建议。
- (void) checkIfInternetEnabled{
Reachability *hostReach = [Reachability reachabilityWithHostName: @"www.apple.com"] ;
NetworkStatus netStatus = [hostReach currentReachabilityStatus];
if (netStatus != ReachableViaWiFi){
NSLog(@"oooaoaoaolaoeworolaowoeoal");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"You must enable wi-fi the first time that you use the application for complete installation " delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
netStatus = [hostReach currentReachabilityStatus];
while(netStatus!= ReachableViaWiFi){
[alert show];
netStatus = [hostReach currentReachabilityStatus];
}
}
}
答案 0 :(得分:3)
您应该收听可达性类发送的有关网络状态更改的通知(您应该使用可达性2.x):
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];