iOS AFN下载

来源:互联网 发布:唐山震后重建数据 编辑:程序博客网 时间:2024/06/05 00:35
#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 ([[NSFileManagerdefaultManager]fileExistsAtPath:moviePath]) {
       
 //调用视频播放器播放本地视频
       
 MPMoviePlayerViewController  *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURLfileURLWithPath: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 = [NSURLURLWithString:@"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:urlcachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:-1];
   
 AFHTTPRequestOperation *operation = [[AFHTTPRequestOperationalloc] 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:[NSURLfileURLWithPath: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
0 0
原创粉丝点击