首页 > 其他 > 详细

BLOCK封装带菊花的网络请求

时间:2015-12-14 18:17:50      阅读:234      评论:0      收藏:0      [点我收藏+]
#import <Foundation/Foundation.h>
@class HttpRequestManager;

typedef void(^httpRequestBlock) (HttpRequestManager *manager);

@interface HttpRequestManager : NSObject

@property(nonatomic,strong)NSData *data;

-(id)initWithUrlString:(NSString *)url  andBlock:(httpRequestBlock)block;

@end
#import "HttpRequestManager.h"

#import "AFNetworking.h"

#import "SVProgressHUD.h"
@implementation HttpRequestManager

- (id)initWithUrlString:(NSString *)url andBlock:(httpRequestBlock)block{
    
    if (self=[super init]) {
        
        [self  requestDataWithString:url andBlock:block];
    }
    
    return self;
}

-(void)requestDataWithString:(NSString *)url  andBlock:(httpRequestBlock)tempBlock{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [SVProgressHUD  showWithStatus:@"正在加载" maskType:SVProgressHUDMaskTypeBlack];
    
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        self.data = responseObject;
        
        [SVProgressHUD  showSuccessWithStatus:@"OK" maskType:SVProgressHUDMaskTypeBlack];
        
        tempBlock(self);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [SVProgressHUD  showErrorWithStatus:@"网速有问题"];
        
        NSLog(@"%@",error.localizedDescription);
        
    }];
    
}
@end
//使用实例

/*
@property(nonatomic,strong)HttpRequestManager *manager;
_manager = [[HttpRequestManager alloc]initWithUrlString:path andBlock:^(HttpRequestManager *manager) {
    
    [cache  saveDataWith:manager.data andNameString:path];
    
    [self  serializationData:manager.data];
}];
*/

 

BLOCK封装带菊花的网络请求

原文:http://www.cnblogs.com/er-dai-ma-nong/p/5045736.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!