iOS开发之普通网络异步请求与文件下载方法

先来说说普通异步下载方法,分为POST、GET两种

/** GET请求获取数据*/
+ (void)getDataWithUrl:(NSString *)strUrl finishBlock:(ECGNCNSDictionaryAndNSErrorBlock)finishBlock
{
    if (strUrl.length == 0) {
        return;
    }
    NSURL *url = [NSURL URLWithString:strUrl];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"GET";
    [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
         //解析并回调数据
        NSDictionary *dicData = [self dictionaryForJsonData:data];
        if (finishBlock) {
            finishBlock(dicData, error);
        }
    }];
    [dataTask resume];
}

/** POST请求获取数据*/
+ (void)postParam:(NSDictionary *)dicParam toUrl:(NSString *)strUrl finishBlock:(ECGNCNSDictionaryAndNSErrorBlock)finishBlock
{
    if (dicParam == nil || strUrl.length == 0) {
        return;
    }
    NSURL *url = [NSURL URLWithString:strUrl];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    request.HTTPBody = [self compactFormatDataForDictionary:dicParam];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        //解析并回调数据
        NSDictionary *dicData = [self dictionaryForJsonData:data];
        if (finishBlock) {
            finishBlock(dicData, error);
        }
    }];
    [dataTask resume];
}

/** 将字典转换成json格式字符串,不含\n这些符号*/
- (NSData *)compactFormatDataForDictionary:(NSDictionary *)dicJson
{
    if (![dicJson isKindOfClass:[NSDictionary class]]) {
        return nil;
    }
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dicJson options:0 error:nil];
    if (![jsonData isKindOfClass:[NSData class]]) {
        return nil;
    }
    
    return jsonData;
}

/** 将二进制数据转换成字典*/
- (NSDictionary *)dictionaryForJsonData:(NSData *)jsonData
{
    if (![jsonData isKindOfClass:[NSData class]] || jsonData.length < 1) {
        return nil;
    }
    
    id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
    if (![jsonObj isKindOfClass:[NSDictionary class]]) {
        return nil;
    }
    return [NSDictionary dictionaryWithDictionary:(NSDictionary *)jsonObj];
}
    

 

下面说说下载文件的方法

/** 下载文件方法*/
- (void)downLoadFileWithUrl:(NSString *)urlStr finishBlock:(GSNetLibTMIdAndDataAndErrorBlock)finishBlock
{
    if (urlStr.length == 0) {
        return;
    }
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLSession *session = [NSURLSession sharedSession];
  //构造下载任务
    NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
    //下载成功回调,文件数据会被默认存储在一个临时目录
        if (error == nil) {
            if (location != nil && [location path].length > 0) {
                NSString *tmpFilePath = [location path];
                NSData *data = [NSData dataWithContentsOfFile:tmpFilePath];
                //回调数据
                if (finishBlock) {
                    finishBlock(0, data, error);
                }
                //删除临时目录文件
      NSFileManager *fileMgr = [NSFileManager defaultManager];
           [fileMgr removeItemAtPath:tmpFilePath error:nil];
            }

        }
    }];
  //执行任务
    [downloadTask resume];
}

 

iOS开发之普通网络异步请求与文件下载方法

上一篇:EIP权限工作流平台-移动端


下一篇:CSS 格式