Qt for Ios uses AFNetWorking to monitor changes in APP network state (for changing caching policies, prompting networks, etc.)

Keywords: network Mobile

Preface

We know that in the process of developing APP, it is still a very common method to monitor the current network status of mobile phones. Here I will accept a method of using AFNet Working to monitor the current network status for you. Network monitoring has a lot of help for program development: for example, using different download strategies, caching strategies, or a single Download Strategy in different network states when downloading. Pure prompt

Use controls

I'm using the version before AFNetWorking 3.0 here. I haven't tested whether the latest version 3.0.4 can be used properly, but I don't think it will affect it. If you have any problems during the testing process, please trust me directly.

code implementation

First, we create a new class inheriting NSObject and write an example.

NetworkingTools.h

#import <Foundation/Foundation.h>
#import "AFNetworking.h"

@interface NetworkingTools : NSObject

+ (AFHTTPRequestOperationManager *)sharedManager;

@end

NetworkingTools.m

#import "NetworkingTools.h"
#import "AFNetworking.h"

@implementation NetworkingTools

+ (AFHTTPRequestOperationManager *)sharedManager {
    static AFHTTPRequestOperationManager * manager = nil;
    if (!manager) {
        manager = [AFHTTPRequestOperationManager manager];
    }

    return manager;
}

@end
  • As you can see, this is basically a very simple example. I believe there is no problem for everyone.  

Next, I'll show you a way to monitor the state of the network, so here I recommend that this method can be monitored in APPDelegate.m.

The following code is the most critical line of code, as long as the implementation of listening to network changes callback is generally no problem.

Keep in mind that the AFHTTP Request Operation Manager in the article was replaced by the AFNetwork Reachability Manager.

My usage:

 self.manager  = [AFNetworkReachabilityManager sharedManager];

//Start listening

 [self.manager startMonitoring];


- (void)listenNetWorkingPort{
    [[NSURLCache sharedURLCache] setMemoryCapacity:5 * 1024 * 1024];
    [[NSURLCache sharedURLCache] setDiskCapacity:50 * 1024 * 1024];

    AFHTTPRequestOperationManager * manager = [NetworkingTools sharedManager];

    // Setting up network state change callback
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:

                // Setting Caching Policy for Network Requests
                manager.requestSerializer.cachePolicy =  NSURLRequestReturnCacheDataDontLoad;
                NSLog(@"Broken network condition");
                //                [UIAlert View show ConfigPrompt: NSLocalizedString (@ No network at present, please check your network settings, nil)];
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:

                // Setting Caching Policy for Network Requests
                manager.requestSerializer.cachePolicy =  NSURLRequestReturnCacheDataElseLoad;
                NSLog(@"4G state");

                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:

                // Setting Caching Policy for Network Requests
                manager.requestSerializer.cachePolicy =  NSURLRequestReloadIgnoringLocalCacheData;
                NSLog(@"WiFi state");
                break;

            default:
                break;
        }
    }];

    // Start network status monitoring
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
}

Epilogue

At present, we are bringing you some simple skills and tools. We hope to communicate with you and make progress together. I would also like to write a more substantial dry goods, but the current level and various constraints, first of all.

Posted by matscott on Tue, 08 Jan 2019 11:24:10 -0800