在我的iOS应用中,我正在尝试监控互联网连接。如果用户未连接到Internet,我会显示警报。但是,我发现警报可能需要很长时间才能看到,大约30秒。如果没有连接用户,还有其他方法可以更快地显示警报吗?
我正在飞机模式下使用iPhone 5S进行测试。
- (void)viewDidLoad
{
[super viewDidLoad];
Reachability *internetConnection = [Reachability reachabilityWithHostname:@"www.google.com"];
// Internet is not reachable
internetConnection.unreachableBlock = ^(Reachability*reach)
{
disconnected = YES;
NSLog(@"no network connection");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No network connection"
message:@"You must be connected to the internet to use this app."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
};
答案 0 :(得分:0)
试试这段代码:
-(void)setUpRechability
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNetworkChange:) name:kReachabilityChangedNotification object:nil];
}
- (void) handleNetworkChange:(NSNotification *)notice
{
NetworkStatus remoteHostStatus = [self.reachability currentReachabilityStatus];
if (!(remoteHostStatus == NotReachable)) {
NSLog(@"Has internet");
}
else
{
NSLog(@"NO internet");
}
}