新建类HttpTool继承NSObject
<code>@interface BAHttpTool : NSObject</code>
一、封装用AFN发送GET请求
<pre><code>
/**
- 发送一个GET请求 用AFN
- @param url 请求路径
- @param params 请求参数
- @param success 请求成功后的回调(请将请求成功后想做的事情写到这个block中)
- @param failure 请求失败后的回调(请将请求失败后想做的事情写到这个block中)
*/
-
(void)Get:(NSString *)url params:(id)params success:(void (^)(id responseObj))success failure:(void (^)(NSError *error))failure;
</code> </pre>
实现代码
<pre><code>
-
(void)Get:(NSString *)url params:(id)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{// 1.获得请求管理者
AFHTTPRequestOperationManager *mar = [AFHTTPRequestOperationManager manager];
// 设置请求格式
[mar GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success) {
success(responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) {
failure(error);
}
}];
}
</code></pre>
使用方法
<pre><code>
[BAHttpTool Get:@"http:xxx.xx.xx" params:nil success:^(id responseObj) {
} failure:^(NSError *error) {
}]
</code></pre>
二、封装用AFN发送POST请求
<pre><code>
- (void)Post:(NSString *)url params:(id)params success:(void (^)(id responseObj))success failure:(void (^)(NSError *error))failure;
</code></pre>
实现代码
<pre><code>
- (void)Post:(NSString *)url params:(id)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
// 1.获得请求管理者
// AFHTTPRequestOperationManager
AFHTTPRequestOperationManager *mar = [AFHTTPRequestOperationManager manager];
[mar POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success) {
success(responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) {
failure(error);
}
}];
}
</code></pre>
使用方法
<pre><code>
[BAHttpTool Post:@"http:xxx.xx.xx" params:nil success:^(id responseObj) {
} failure:^(NSError *error) {
}]
</code></pre>
封装AFN网络加载指示器
<pre><code>
typedef NS_ENUM(NSInteger, NetworkReachabilityStatus) {
NetworkReachabilityStatusUnknown = -1,
NetworkReachabilityStatusNotReachable = 0,
NetworkReachabilityStatusReachableViaWWAN = 1,
NetworkReachabilityStatusReachableViaWiFi = 2,
};
+(void)setReachabilityStatusChangeBlock:(void(^)(NetworkReachabilityStatus status))block;
+(void)stopMonitoring;
</code></pre>
实现代码
<pre><code>
+(void)setReachabilityStatusChangeBlock:(void(^)(NetworkReachabilityStatus status))block{
//监测网络状态
AFNetworkReachabilityManager *netMan = [AFNetworkReachabilityManager sharedManager];
[netMan setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusUnknown: // 未知网络
if (block) {
block(NetworkReachabilityStatusUnknown);
}
break;
case AFNetworkReachabilityStatusNotReachable: // 没有网络
if (block) {
block(NetworkReachabilityStatusNotReachable);
}
break;
case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
if (block) {
block(NetworkReachabilityStatusReachableViaWWAN);
}
break;
case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
if (block) {
block(NetworkReachabilityStatusReachableViaWiFi);
}
break;
}
}];
[netMan startMonitoring];
}
</code></pre>
<pre><code>
+(void)stopMonitoring
{
AFNetworkReachabilityManager *netMan = [AFNetworkReachabilityManager sharedManager];
[netMan stopMonitoring];
}
</code></pre>