如何在iOS中检查Internet是否正常工作 [英] How to check Internet is working or not in ios

查看:171
本文介绍了如何在iOS中检查Internet是否正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 AFNetworking 来检测设备上的互联网是否正常工作;请参阅SO
AFNetworking 2.0可达性上的答案。我能够检测到设备已连接WiFi或3G。

I am trying to detect the internet is working or not on the device using AFNetworking ; referred to the answer here on the SO AFNetworking 2.0 Reachability . I am able to detect device is connected with WiFi or 3G..

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

The problem is Actually internet is not working like the WIFI is on but internet is not working.

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

How Can i detect internet is working .Like we check using 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;

}

推荐答案

添加用于更改网络状态的通知观察器。
就像在完成启动类下的应用程序委托类中添加以下代码

Add notification observer for change in network status. Like add following code in app delegate class under finish launching class

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

在.h文件中添加全局Alertview。

Add global alertview in .h file. as it will appear and disappear on network status change.

这是在以下位置调用的通知功能:

here is notification function called on :

- (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];
    }
}

这篇关于如何在iOS中检查Internet是否正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆