zoukankan      html  css  js  c++  java
  • AFNetworking监控网络状态以及下载的进度条显示

    注:使用AFNetworking加载数据时,首先要添加HTTPs的白名单,设置详解见http://www.cnblogs.com/h-tao/p/5098877.html

    判断网络状态:

    #pragma mark - 判断网络状态
    - (void)judgeNetworkStatus
    {
        //原理:看能不能和一个网址连接上;
        AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"www.baidu.com"]];
        
        //设置网络状态改变的时候 执行的block  参数传入网络状态
    
        [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            NSArray *arr = @[@"为止",@"不可达",@"wps",@"wifi"];
            
            NSLog(@"%@",arr[status + 1]);
        }];
        
        //开始监控网络状态
        [manager.reachabilityManager startMonitoring];
        
    }

    下载进度条显示:

    #pragma mark - 下载进度,用进度条表达
    - (void)textDownload
    {
        NSString *urlStr = @"http://imgcache.qq.com/club/item/avatar/zip/7/i87/all.zip";
    
        //注意:下载进度一般使用AFURLSessionManager
        AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
        
        //参数一:传入URL请求
        //参数二:传入进度对象的地址,使用KVO监控下载进度
        //参数三:block中返回下载文件保存的地址
        NSProgress *progress = nil;
        
        
        NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
            
            //返回文件下载保存的地址
            NSString *path = [NSString stringWithFormat:@"%@/Documents/text.zip",NSHomeDirectory()];
            NSLog(@"%@",path);
            
            return [NSURL fileURLWithPath:path];
            
        } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
            
            NSLog(@"下载完成");
            
            NSLog(@"filePath = = = %@",filePath);
        }];
        
        
        //监控下载进度
        [progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
        
        _progress = progress;
        
        _progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(20, 100, 200, 20)];
    
        [self.view addSubview:_progressView];
        
        //开始启动下载任务
        [task resume];
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        double fractionCompleted = [[object valueForKey:keyPath] doubleValue];
        NSLog(@"下载进度:%f%%",fractionCompleted);
        
        //这个方法在子线程中执行,子线程中不能直接操作UI;
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            _progressView.progress = fractionCompleted;
        }];
    }
    
    
    
    -(void)dealloc
    {
        [_progress removeObserver:self forKeyPath:@"fractionCompleted"];
    }
  • 相关阅读:
    Ubuntu完全教程,让你成为Ubuntu高手!
    Centos7安装完毕后重启提示Initial setup of CentOS Linux 7 (core)的解决方法
    MS SQL操作Xml示例
    MY SQL sql_mode设置
    MS SQL " 无法启动分布式事务"问题的解决思路
    MS SQL常用系统表汇总
    SQL不同服务器数据库之间数据操作整理
    OPENQUERY用法
    SQL Compare 10.4.8.87 Edition 数据库比较工具 完全破解+使用教程
    各种主流 SQLServer 迁移到 MySQL 工具对比
  • 原文地址:https://www.cnblogs.com/h-tao/p/5098900.html
Copyright © 2011-2022 走看看