一、使用Cocoapods库管理工具下载AFNetworking框架

设置你的Cocoapods

先要确保你已经安装了Cocoapods。为此,打开命令行程序,并输入。

  1. which pod  
你将会看到类似这样的输出:

  1. /usr/bin/pod 
如果命令行简单的返回提示,或显示pod not found,表示Cocoapods未安装在你的机器上

使用一下命令完成安装:

sudo gem install cocoapods
二、创建一个工程:

例如我们创建一个名为Networking的工程:

然后使用终端切换到工程目录,当前目录下应该有一个.xcodeproj的文件

创建Podfile文件,使用命令:

touch Podfile

打开Podfile文件,添加配置信息:

vim Podfile

使用vim编辑器。输入i进入输入模式,添加如下信息:

platform:ios,'9.0'
target 'Networking' do
pod 'AFNetworking'
end

按下esc,然后输入:wq回车,这是保存并退出命令

在命令行中输入pod install进行安装。

等待下载完成,现在进入我们刚创建的那个工程项目,这时候会发现一个.xcworkspace的文件,打开这个文件。

这时候AFNetworking框架就添加到了我们工程中

ViewController.m中导入框架并定义一个URL,这是一首歌曲下载链接

#import <AFNetworking.h>

#define MP3_URL @"http://so1.111ttt.com:8282/2016/5/12m/10/205101338233.m4a?tflag=1504768534&pin=0b4c71c40af30e63e9b5abef4bf5c3b5&ip=111.85.134.190#.mp3"

@interface ViewController ()

@end


创建一个方法:

-(void)downloadTask {

    //1.定义一个管理器

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    //下载任务

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:MP3_URL]];

   

//    progress参数:AFNetWorking 3.0 之后传入的将是block,进度操作在block中完成

    //进度

    /*

     @property int64_t totalUnitCount; 总共的

     @property int64_t completedUnitCount; 下载的数据长度

     */

    

    NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress *downloadProgress){

        // 使用KVO检测进度

        [downloadProgress addObserver:self forKeyPath:@"completedUnitCount" options:NSKeyValueObservingOptionNew context:nil];

    

    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {

//        NSLog(@"--%@--", targetPath);

       

       //返回下载文件存放位置

       NSString *location = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:response.suggestedFilename];

//       NSLog(@"下载位置=%@",[NSURL fileURLWithPath:location]);

        return [NSURL fileURLWithPath:location];

       

    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {

//        NSLog(@"---filePath=%@",filePath);

//        NSLog(@"error = %@",error);

    }];

 

    

    // 开启任务

    [task resume];

}

//检测进度方法

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context

{

    //强制类型转换

    NSProgress *progress = (NSProgress *)object;

    NSLog(@"下载进度 = %f%%",100.0*progress.completedUnitCount / progress.totalUnitCount);

}


// 点击屏幕后调用方法:

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    [self downloadTask];

}


打印结果:

iOS 05-使用AFNetworking框架创建下载任务

相关文章:

  • 2022-12-23
  • 2021-11-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-18
  • 2021-07-15
  • 2021-06-02
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-10
  • 2022-12-23
相关资源
相似解决方案