测试Internet连接iOS

测试Internet连接iOS,ios,objective-c,http,connection,Ios,Objective C,Http,Connection,我正在使用Tony Million的可达性 我有一个Connection.m类,我有以下函数: + (void)testBasicInternetConnection { Reachability *internetReachability; internetReachability = [Reachability reachabilityWithHostName:@"www.google.com"]; //Internet is not reachable

我正在使用Tony Million的可达性

我有一个Connection.m类,我有以下函数:

+ (void)testBasicInternetConnection {

    Reachability *internetReachability;
    internetReachability = [Reachability reachabilityWithHostName:@"www.google.com"];

    //Internet is not reachable
    internetReachability.unreachableBlock = ^(Reachability*reach) {
        //Alert the user
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Attention" message:@"You are not connected to the internet. Please check your connection" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        });
    };

    //Internet is reachable
    internetReachability.reachableBlock = ^(Reachability *reach) {
        NSLog(@"Internet Access: SUCCESSFUL");
    };

    [internetReachability startNotifier];}
我真正想做的是从该函数返回一个不可访问或可访问的BOOL

因此,在我的ViewController中,我想做如下操作:

if ([Connection testBasicInternetConnection]) {
    [self logIn];
}
我一直在琢磨如何让它把那玩意儿还回去


谢谢。

在appdelegate中使用可达性并在那里实现委托。在appdelegate中创建一个标志,并在viewController中选中该标志。在appdelegate中rechachablitychanged中更改标志。或者在您的连接类中使用它-(void)reachabilityChanged:(SCNetworkReachabilityFlags)标志实现此方法查看此处:,它具有
-(BOOL)isReachable
-(BOOL)可通过WWAN删除
-(BOOL)可通过WiFi连接函数。@gabbler-我看到了。我相信仅仅使用它们的问题是unreachableBlock和reachableBlock当然不会马上完成,因此if(isReachable){}每次都会失败,因为它还没有完成。请参考以下内容:,它在应用程序启动时创建了一个可达性管理器,并开始监控,然后在单独的控制器中,您可以立即获得状态。