使用 AFNetworking 检查互联网连接状态

Check Internet connection state using AFNetworking

我想创建通用 class 来检查互联网连接,当互联网状态改变时它应该通知我。

我正在使用 AFNetworking 检查互联网状态。

这是我试过的代码,但它不起作用请帮助我哪里出错了??

CheckInternet 是我常用的 class 来自 NSObject

CheckInternet.h

#import <Foundation/Foundation.h>

@interface CheckInternet : NSObject

+ (void)startNetworkMonitoring;
+ (BOOL)isInternetConnectionAvailable;
@end

CheckInternet.m

#import "CheckInternet.h"
#import "AFNetworking.h"

@implementation CheckInternet


+ (void)startNetworkMonitoring
{
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));

        // Check the reachability status and show an alert if the internet connection is not available
        switch (status) {
            case -1:
                // AFNetworkReachabilityStatusUnknown = -1,
                NSLog(@"The reachability status is Unknown");
                break;
            case 0:
                // AFNetworkReachabilityStatusNotReachable = 0
                NSLog(@"The reachability status is not reachable");
                break;
            case 1:
                NSLog(@"The reachability status is reachable via wan");
                [[NSNotificationCenter defaultCenter] postNotificationName:@"reachabilityChanged" object:nil];
                break;
            case 2:
                // AFNetworkReachabilityStatusReachableViaWiFi = 2
                NSLog(@"The reachability status is reachable via WiFi");

                [[NSNotificationCenter defaultCenter] postNotificationName:@"reachabilityChanged" object:nil];
                break;

            default:
                break;
        }

    }];
}

#pragma mark - Check Internet Network Status
+ (BOOL)isInternetConnectionAvailable {
    return [AFNetworkReachabilityManager sharedManager].reachable;
}

@end

在我的 ViewController.m 中(我想在这里检查互联网状态)

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];



     [CheckInternet startNetworkMonitoring];


    if ([CheckInternet isInternetConnectionAvailable])
    {
         NSLog(@"---- Internet YES------ ");
    }
    else
    {
         NSLog(@"----- Internet NO------ ");
    }
}

//This is not called...
- (void)reachabilityChanged:(NSNotification *)notification
{
    NSLog(@"---- reachabilityChanged ----");
}

先谢谢了!

首先,我建议在 AppDelegate 中开始监控您的网络 activity。将 [CheckInternet startNetworkMonitoring]; 移动到 didFinishLaunchingWithOptions

其次,将以下行添加到 case == 0:

 [[NSNotificationCenter defaultCenter] postNotificationName:@"reachabilityChanged" object:[NSNumber numberWithInteger:one]];

还要确保您 post 与通知的连接状态。因为它是一个整数,所以你需要把它包装在一个对象中(通常是包装成NSNumber)。

第三,您需要观察reachabilityChanged通知。 将此添加到您的 viewDidLoad.

 [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(reachabilityChanged:) 
        name:@"reachabilityChanged"
        object:nil];

在您的函数 - (void)reachabilityChanged:(NSNotification *)notification 中,您应该能够访问通知的用户信息 属性 并在那里读取状态代码。

此外,请不要忘记在您的 viewController 消失后取消关注通知。所以在 dealloc

[NSNotificationCenter defaultCenter] removeObserver:self];