对AFN网络请求进行封装,代码如下:
/************************* .h文件 *************************/
#import <Foundation/Foundation.h>
@interface HWManagerTool : NSObject
/**
* 发送一个POST请求
*
* @param url 请求路径
* @param params 请求参数
* @param success 请求成功后的回调
* @param failure 请求失败后的回调
*/
+ (void)postWithURL:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure;
/**
* 发送一个GET请求
*
* @param url 请求路径
* @param params 请求参数
* @param success 请求成功后的回调
* @param failure 请求失败后的回调
*/
+ (void)getWithURL:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure;
@end
/************************* .m文件 *************************/
#import "HWManagerTool.h"
#import "AFNetworking.h"
@implementation HWManagerTool
+ (void)postWithURL:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
// 创建请求管理对象
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
session.responseSerializer = [AFJSONResponseSerializer serializer];
session.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
// 设置超时时间
[session.requestSerializer willChangeValueForKey:@"timeoutInterval"];
session.requestSerializer.timeoutInterval = 30.f;
[session.requestSerializer didChangeValueForKey:@"timeoutInterval"];
// 发送请求
[session POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
}
+ (void)getWithURL:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
// 创建请求管理对象
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
session.responseSerializer = [AFJSONResponseSerializer serializer];
session.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
// 设置超时时间
[session.requestSerializer willChangeValueForKey:@"timeoutInterval"];
session.requestSerializer.timeoutInterval = 30.f;
[session.requestSerializer didChangeValueForKey:@"timeoutInterval"];
// 发送请求
[session GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
}
@end
对项目网络请求进行封装,代码如下:
/************************* .h文件 *************************/
#import <Foundation/Foundation.h>
@interface HWNetwork : NSObject
// 登录
+ (void)postLoginInfoWithUsername:(NSString *)username password:(NSString *)password success:(void (^)(id json))success failure:(void (^)(NSError *error))failure;
// 首页信息
+ (void)getHomeInfoSuccess:(void (^)(id json))success failure:(void (^)(NSError *error))failure;
@end
/************************* .m文件 *************************/
#import "HWNetwork.h"
#import "HWManagerTool.h"
#define KIP @"https://api.ceshi.com"
// 登录
#define KLoginInfo [NSString stringWithFormat:@"%@/index.php?s=public/login", KIP]
// 首页信息
#define KHomeInfo [NSString stringWithFormat:@"%@/index.php?s=public/home", KIP]
@implementation HWNetwork
// 登录
+ (void)postLoginInfoWithUsername:(NSString *)username password:(NSString *)password success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setValue:username forKey:@"phone"];
[params setValue:password forKey:@"password"];
[HWManagerTool postWithURL:KLoginInfo params:params success:^(id json) {
if (success) {
success(json);
}
} failure:^(NSError *error) {
if (failure) {
failure(error);
}
}];
}
// 首页信息
+ (void)getHomeInfoSuccess:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
[HWManagerTool getWithURL:KHomeInfo params:nil success:^(id json) {
if (success) {
success(json);
}
} failure:^(NSError *error) {
if (failure) {
failure(error);
}
}];
}
@end
控制器调用:
- (void)getInfo
{
// 登录
[HWNetwork postLoginInfoWithUsername:@"asdf" password:@"123456" success:^(id json) {
} failure:^(NSError *error) {
}];
// 获取首页信息
[HWNetwork getHomeInfoSuccess:^(id json) {
} failure:^(NSError *error) {
}];
}
利用AFN监听网络改变,封装为单利,代码如下:
/************************* .h文件 *************************/
#import "AFNetworkReachabilityManager.h"
@interface HWNetworkReachabilityManager : NSObject
// 当前网络状态
@property (nonatomic, assign, readonly) AFNetworkReachabilityStatus networkReachabilityStatus;
// 获取单例
+ (instancetype)shareManager;
// 监听网络状态
- (void)monitorNetworkStatus;
/************************* .m文件 *************************/
#import "HWNetworkReachabilityManager.h"
@interface HWNetworkReachabilityManager ()
@property (nonatomic, assign, readwrite) AFNetworkReachabilityStatus networkReachabilityStatus;
@end
@implementation HWNetworkReachabilityManager
+ (instancetype)shareManager
{
static HWNetworkReachabilityManager *manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[self alloc] init];
});
return manager;
}
// 监听网络状态
- (void)monitorNetworkStatus
{
// 创建网络监听者
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusUnknown:
// 未知网络,这里可以处理公共网络变更事件
HWLog(@"当前网络:未知网络");
break;
case AFNetworkReachabilityStatusNotReachable:
// 无网络
HWLog(@"当前网络:无网络");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
// 蜂窝数据
HWLog(@"当前网络:蜂窝数据");
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
// 无线网络
HWLog(@"当前网络:无线网络");
break;
default:
break;
}
if (_networkReachabilityStatus != status) {
_networkReachabilityStatus = status;
// 网络改变通知,单独网络变更事件可以添加通知实现
[[NSNotificationCenter defaultCenter] postNotificationName:HWNetworkingReachabilityDidChangeNotification object:[NSNumber numberWithInteger:status]];
}
}];
// 开始监听
[manager startMonitoring];
}
@end
在appDelegate中开启监听:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 开启网络监听
[[HWNetworkReachabilityManager shareManager] monitorNetworkStatus];
return YES;
}
其它控制器添加通知获取事件:
// 网路改变通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkingReachabilityDidChange:) name:HWNetworkingReachabilityDidChangeNotification object:nil];
#pragma mark - HWNetworkingReachabilityDidChangeNotification
- (void)networkingReachabilityDidChange:(NSNotification *)notification
{
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}