首页 > 代码库 > 封装 block 请求网络数据

封装 block 请求网络数据



NetWorking.h


#import <Foundation/Foundation.h>

typedef void (^FinishBlock)(id responseObject);

@interface NetWorking : NSObject


@property (nonatomic ,strong) FinishBlock finishBlock;


+(void)GetDataWithURL:(NSString *)url finshedBlock:(FinishBlock)block;


@end




NetWorking.m


#import "NetWorking.h"
#import "AFNetworking.h"
@implementation NetWorking



+(void)GetDataWithURL:(NSString *)url finshedBlock:(FinishBlock)block
{
    NetWorking * request = [[NetWorking alloc]init];
    request.finishBlock = block;
    
    AFHTTPRequestOperationManager *manager =
    [AFHTTPRequestOperationManager manager];
    [manager GET:url
      parameters:nil
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             if (request.finishBlock) {
                 request.finishBlock(responseObject);
             }
             
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
         }];
}
@end

封装 block 请求网络数据