封装 block 请求网络数据
更新:HHH   时间:2023-1-7




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

返回开发技术教程...