托尼·米利恩可达性(Reachability)在连接时显示不可达

10

我已经搜索过了,但没有找到和我的问题类似的情况。 我相信这是我忽视了某些东西。

我正在使用Tony Million的可达性块方法。 当我有互联网连接时,它工作得很好,没有互联网连接时,警报会弹出并正常工作。

但是,当我没有互联网连接,然后重新连接上互联网时,同样的警报弹出。

我的代码如下:

-(void)reachabilityBlock
{
// allocate a reachability object
Reachability * reach = [Reachability reachabilityWithHostname:@"www.google.com"];

// tell the reachability that we DONT want to be reachable on 3G/EDGE/CDMA
reach.reachableOnWWAN = YES;


reach.reachableBlock = ^(Reachability * reachability)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        //NSLog(@"REACHABLE! block");
        [self newsTableViewRefresher];
    });
};

reach.unreachableBlock = ^(Reachability * reachability)
{

        dispatch_async(dispatch_get_main_queue(), ^{
            //NSLog(@"UNREACHABLE! block");

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"No Network found!"
                                                            message: @"You have no wifi or cellular connection available. Please connect to a WIFI or cellular network."
                                                           delegate: self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];

            [alert show];


        });

};


[reach startNotifier];
[self.refreshControl endRefreshing];

 }
我的问题是为什么在我连接到互联网时会弹出"无法访问"的警告提示?
谢谢您的时间。

在显示警告之前,请尝试检查不可达块中的可达性参数。同时,将其记录下来以查看哪个块首先被调用。 - Joel Fischer
3
你最终解决了这个问题吗?我也遇到了完全相同的问题。 - jdog
2
@jgervin 我使用[Reachability reachabilityForInternetConnection];比使用[Reachability reachabilityWithHostname:@"www.google.com"];获得更好的结果。 - Alec
谢谢,我会试一下。 - jdog
@Alec 这也对我有帮助! - Sound Blaster
1个回答

2

这是我在我的端上所做的,并为我完成了工作。我尝试使用块,但似乎更麻烦而不是解决我的问题。希望这可以帮到你。

Reachability *reach = [Reachability reachabilityWithHostname:@"www.jportdev.com"];
if ([reach isReachable]){
    // Reachable
    //NSLog(@"is reachable.......");
}else{
    // not Reachable
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network is unavaliable!" message:@"Some content in this application might not be avaliable without network connectivity." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    alert = nil;
}

2
好的,但是如果连接中断了,你没有块来支持你,你要怎么返回这个位置呢? - Mirko Catalano
对我来说,即使 isReachable 报告的值是错误的(当从离线转为在线时),当网络可用且目标地址可达时。但我正在使用 iOS 模拟器,所以不确定这是否是一种受支持的工作方式。 - The Senator

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接