当前位置:  开发笔记 > 编程语言 > 正文

如何在ios中显示afnetworking 3.0的进度条?

如何解决《如何在ios中显示afnetworking3.0的进度条?》经验,为你挑选了0个好方法。

我想将待处理数据上传到服务器,然后在同一按钮点击下载服务器数据库中的所有数据.但是我没有得到如何显示进度条显示上传/下载的进度.我尝试使用"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);
    //     });

    //  }
}];

有人请帮帮我!谢谢!

推荐阅读
云聪京初瑞子_617
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有