【发布时间】:2014-09-07 20:38:21
【问题描述】:
在我的 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];
};
【问题讨论】:
-
不使用主机名检查连接。请改用 SystemConfiguration 框架。看看安德鲁斯的回答,这个问题有点低 - stackoverflow.com/questions/1083701/…
-
感谢您的建议 - 这对我有用!
标签: ios objective-c reachability