iOS AFN下载

#import  "ViewController.h"
#import
  "AFNetworking.h"
#import
 
@interface  ViewController  ()
@property  ( strong ,  nonatomic )  IBOutlet  UIView  *progressView;
@property  ( weak ,  nonatomic )  IBOutlet  UIProgressView  *progress;
@property  ( weak ,  nonatomic )  IBOutlet  UIButton  *button;

@end

@implementation  ViewController
- (
IBAction )ButtonTap:( id )sender {
   
  // 使用 AFN 进行下载

   
   
}
- (
IBAction )ButtonTapa:( UIButton  *)sender {
   
  // 指定数据下载到那个文件当中
   
  NSString  *cachePath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory ,  NSUserDomainMask , YES ) firstObject ];
   
  // 声明文件的路径
   
  NSString  *moviePath = [cachePath  stringByAppendingPathComponent : @"mo.mp4" ];
   
   
  // 判断 moviePath 路径下到底有没有文件
   
  if  ([[ NSFileManager defaultManager ] fileExistsAtPath :moviePath]) {
       
  // 调用视频播放器播放本地视频
       
  MPMoviePlayerViewController   *moviePlayer = [[ MPMoviePlayerViewController  alloc ]  initWithContentURL :[ NSURL fileURLWithPath :moviePath]]; //NSURL fileURLWithPath:moviePath 本地文件的地址
       moviePlayer.
moviePlayer . scalingMode  = MPMovieScalingModeAspectFit ;
       moviePlayer.
moviePlayer . controlStyle  = MPMovieControlStyleEmbedded ;
        moviePlayer.
view . frame  =  CGRectMake ( 5 ,  70 , self . view . frame . size . width - 10 ,  200 );
        [
self  presentMoviePlayerViewControllerAnimated :moviePlayer];
       
  return ;
    }
   
  // 使用 AFN 进行下载
   
  // 创建操作对象 //http://live.dl.ltimg.net/livezip/download/5540385469401b10912f7a24?type=mp4
   
  // -1 就是不设置超时时间 ,  一般是 30
   
  NSURL  *url = [ NSURL URLWithString : @"http://api.sina.cn/sinago/video_location.json?sf_i=4&video_id=138600506&fromsinago=1&postt=news_video_video_1&from=" ];
   
  NSURLRequest  *urlRequest = [ NSURLRequest  requestWithURL :url cachePolicy : NSURLRequestUseProtocolCachePolicy timeoutInterval :- 1 ];
   
  AFHTTPRequestOperation  *operation = [[ AFHTTPRequestOperation alloc ]  initWithRequest :urlRequest];
   
  //  第一个参数    下载文件的路径
   
  //             是否接着写入文件 ,  用来做断点下载
    operation.
outputStream  = [[ NSOutputStream  alloc ] initToFileAtPath :moviePath  append : NO ];
   
   
  // 这个方法就是下载的过程 ,  用来做进度条
   
  // 第一个参数    本次下载了多少数据
   
  //           一共下载了多少数据
   
  //           这个文件或者视频 ,  音频一共有多大
    [operation
  setDownloadProgressBlock :^( NSUInteger  bytesRead, long  long  totalBytesRead,  long  long  totalBytesExpectedToRead) {
       
  // 进度条显示进度
       
  self . progress . progress  = ( float )totalBytesRead/totalBytesExpectedToRead;
       
    }];
   
   
  // 下载完成后的方法
    [operation
  setCompletionBlockWithSuccess :^( AFHTTPRequestOperation  *operation,  id  responseObject) {
       
       
  // 调用视频播放器播放本地视频
       
  MPMoviePlayerViewController   *moviePlayer = [[ MPMoviePlayerViewController  alloc ]  initWithContentURL :[ NSURL fileURLWithPath :moviePath]]; //NSURL fileURLWithPath:moviePath 本地文件的地址
        [
self  presentMoviePlayerViewControllerAnimated :moviePlayer];
       
       
    }
  failure :^( AFHTTPRequestOperation  *operation,  NSError  *error) {
       
  NSLog ( @" 抱歉 , 下载失败 " );
    }];
   
  // 打开这个任务
    [operation
  start ];
  
   
}

- (
void )viewDidLoad {
    [
super  viewDidLoad ];
   
  // Do any additional setup after loading the view, typically from a nib.
}

- (
void )didReceiveMemoryWarning {
    [
super  didReceiveMemoryWarning ];
   
  // Dispose of any resources that can be recreated.
}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值