【问题标题】:How to check Internet is working or not in ios如何在 ios 中检查 Internet 是否正常工作
【发布时间】:2015-04-17 20:17:39
【问题描述】:

我正在尝试使用 AFNetworking 检测设备上的互联网是否正常工作;在SO上提到了这里的答案 AFNetworking 2.0 Reachability 。我能够检测到设备是否与 WiFi 或 3G 连接..

问题是实际上互联网无法正常工作,就像 WIFI 开启但互联网无法正常工作。

我如何检测互联网是否正常工作。就像我们使用 ping 进行检查....

我正在使用以下方法进行检查

-(BOOL)connected {

  __block BOOL reachable;

// Setting block doesn't means you area running it right now
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

    switch (status) {
        case AFNetworkReachabilityStatusNotReachable:
            NSLog(@"No Internet Connection");
            reachable = NO;
            break;
        case AFNetworkReachabilityStatusReachableViaWiFi:
            NSLog(@"WIFI");

            reachable = YES;
            break;
        case AFNetworkReachabilityStatusReachableViaWWAN:
            NSLog(@"3G");
            reachable = YES;
            break;
        default:
            NSLog(@"Unkown network status");
            reachable = NO;
            break;

    }
}];
// and now activate monitoring
[[AFNetworkReachabilityManager sharedManager] startMonitoring];

return reachable;

}

【问题讨论】:

  • 如果需要防火墙登录而不是在设备连接到 wifi 或 3g 的情况下无法访问互联网。
  • 在我的情况下不需要登录,但即使需要登录,我也需要检查当发现 AFNetworkReachabilityStatusReachableViaWiFi 为 TRUE 时,我是否能够从设备 ping 说 google.com
  • 你可以检查同样的问题:stackoverflow.com/questions/15146607/…

标签: ios afnetworking-2


【解决方案1】:

为网络状态的变化添加通知观察者。 就像在完成启动类下的应用委托类中添加以下代码

 [[AFNetworkReachabilityManager sharedManager] startMonitoring];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNetworkChnageNotification:) name:AFNetworkingReachabilityDidChangeNotification object:nil];

在 .h 文件中添加全局警报视图。因为它会随着网络状态的变化而出现和消失。

这里是调用的通知函数:

- (void)receiveNetworkChnageNotification:(NSNotification *)notification
{
    if (![self isReachable])
    {
        self.alertView = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"You have no internet connection on your device, please check and try again later." delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [self.alertView show];
    }
    else
    {
        [self.alertView dismissWithClickedButtonIndex:0 animated:YES];
    }
}

【讨论】:

    【解决方案2】:

    您可以使用可达性来确定您是否与网络建立了正常连接。但是,如果您出于某种原因确实需要确定您有一个可行的互联网连接,那么最好的方法是尝试建立一个实际的连接并检查结果。为此使用 NSURLConnection。

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSMutableData *receivedData = [NSMutableData dataWithCapacity:0];
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    
    if(!theConnection) { // Something went wrong.
        receivedData = nil;
    }
    

    要确定您是否建立了可行的连接,请使用委托方法检查连接结果:

    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
        NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription],
            [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-10
      • 2015-07-25
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      • 2014-01-11
      • 2020-04-23
      • 1970-01-01
      相关资源
      最近更新 更多