【问题标题】:Reachability & Alerts可达性和警报
【发布时间】: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


【解决方案1】:

试试这个代码:

-(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");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    相关资源
    最近更新 更多