AFNetworkReachabilityManager始终返回NO

时间:2017-06-15 04:16:28

标签: ios objective-c

[[AFNetworkReachabilityManager sharedManager] startMonitoring];
AFNetworkReachabilityManager*manger =[AFNetworkReachabilityManager sharedManager];
BOOL is=[manger isReachable];

为什么它一直没有返回?我知道我用wifi连接互联网。

4 个答案:

答案 0 :(得分:0)

需要一些时间来确定网络状态。

因此,在startMonitoring之后 isReachable 将始终返回 false

您需要在 setReachabilityStatusChangeBlock 中检查 isReachable ;

    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status){
          NSLog(@"network staus changed");
         //check for network related flag
    }];

答案 1 :(得分:0)

[[AFNetworkReachabilityManager sharedManager] startMonitoring]将花费一点时间来返回正确的值。

我使用此代码,这对我有用!

[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"Rechability : %@", AFStringFromNetworkReachabilityStatus(status));

        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:
            {
                // NO Internet Connection.
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWWAN:
            {

                break;
            }
            case AFNetworkReachabilityStatusReachableViaWiFi:
            {

                break;
            }
            case AFNetworkReachabilityStatusUnknown:
            {

                break;
            }
            default: break;
        }
    }];
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];

答案 2 :(得分:0)

执行以下步骤,这是我项目中的工作代码。

第1步:

//AppDelegate.m

[self startMonitoringTheNetwork];// Call this method didFinishLaunchingWithOptions

第2步:

#pragma mark - Interenet Connection Status

-(void)startMonitoringTheNetwork {

    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

        switch (status) {

            case AFNetworkReachabilityStatusUnknown:
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                //available
                break;
            case AFNetworkReachabilityStatusNotReachable:
                //not available
                break;
            default:
                break;
        }
    }];
    //start monitoring
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
}

第3步:

  

将以下类级别方法添加到CommonClass.h中       +(BOOL)isInternetConnected;

第4步:

  

并将上述方法实现到CommonClass.m

+(BOOL) isInternetConnected {

    BOOL isConnected = NO;

    if (!isConnected) {
        isConnected = [AFNetworkReachabilityManager sharedManager].reachable;
    }

    return isConnected;
}

第5步:

  

现在您已准备好通过导入CommonClass.h来访问互联网状态,即#import" CommonClass.h"

Bool isInternetConnected = [CommonClass isInternetConnected];

注意:第3步& 4不是强制性的,如果需要,可以直接访问。

答案 3 :(得分:0)

In the latest Version of AFNetworking,

Reachability should be monitered when the app is launched,ie

you should write the below code in the AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

method, so that the Rechability manager starts monitering the status when the application starts.

[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"Rechability : %@", AFStringFromNetworkReachabilityStatus(status));

        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:
            {
                // NO Internet Connection.
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWWAN:
            {

                break;
            }
            case AFNetworkReachabilityStatusReachableViaWiFi:
            {

                break;
            }
            case AFNetworkReachabilityStatusUnknown:
            {

                break;
            }
            default: break;
        }
    }];
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];

If you want to know the status before performing some operation you can use:

[[AFNetworkReachabilityManager sharedManager] isReachable];

If you want to know immediately if network is cut off, then you can post a NSNotification so that you can receive it anywhere and perform some operation