• BLOCK封装带菊花的网络请求


    #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];
    }];
    */
  • 相关阅读:
    显示和隐藏密码
    如何给input的右上角加个清除的按钮?
    手机号中间四位用*号代替
    利用JS+正则表达式获取URL的GET数据
    腾讯QQ头像/QQ网名等相关获取API接口
    jqurey 在编辑的时候为select设置选中项
    html面试题
    js splice和delete删除数组长度会变化吗
    webp与jpg、png比较,它有什么优劣势?如何选择?
    iOS开发技术之应用代码注入防护
  • 原文地址:https://www.cnblogs.com/er-dai-ma-nong/p/5045736.html
Copyright © 2020-2023  润新知