我想将待处理数据上传到服务器,然后在同一按钮点击下载服务器数据库中的所有数据.但是我没有得到如何显示进度条显示上传/下载的进度.我尝试使用"setDownloadTaskDidWriteDataBlock"但它永远不会被调用.我的代码:
-(void)asyncTask:(NSString *)strURL parameters:(NSDictionary *)dictParams success:(void (^)(NSDictionary *response))success failure:(void (^)(NSError *error))failure { NSLog(@"parameters passed to server through services=%@",dictParams); AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; // [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil]; [manager POST:strURL parameters:dictParams progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"inside requestPostUrl JSON: %@", responseObject); if([responseObject isKindOfClass:[NSDictionary class]]) { if(success) { success(responseObject); } } else { NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil]; if(success) { success(response); } } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { if(failure) { failure(error); } }];
//永远不会触发以下方法
[manager setDownloadTaskDidWriteDataBlock:^(NSURLSession *session,NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) { CGFloat written = totalBytesWritten; CGFloat total = totalBytesExpectedToWrite; CGFloat percentageCompleted = written/total; NSLog(@"percentage completed=%f",percentageCompleted); dispatch_async(dispatch_get_main_queue(), ^{ // here is what you want // vc.progressBarView.progress = (CGFloat)totalBytesWritten / totalBytesExpectedToWrite; }); //Return the completed progress so we can display it somewhere else in app // if( progressBlock){ // dispatch_async(dispatch_get_main_queue(), ^{ // progressBlock(percentageCompleted,remoteURLPath); // }); // } }];
有人请帮帮我!谢谢!