码迷,mamicode.com
首页 > Web开发 > 详细

利用NSURLSession在网络里下载视频

时间:2015-06-11 16:51:43      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:

- (IBAction)StartDownLoad:(UIButton *)sender {
    
    //NSString *urlstr = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *urlstr= @"http://www.letv.com/ptv/vplay/22929585.html?vfm=bdvppzq&bl=hb#frp=v.baidu.com%2Fshow_intro%2F";
    urlstr = [urlstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSURL *url = [NSURL URLWithString:urlstr];
    
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    NSURLSession * session = [NSURLSession sessionWithConfiguration:config];
    
  NSURLSessionDownloadTask * task =  [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
      NSHTTPURLResponse *httpresponse =(NSHTTPURLResponse*)response;
      if (httpresponse.statusCode==200) {
          
         // NSLog(@"%@",location);
          
        NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
          NSString *filepath = [path stringByAppendingPathComponent:@"down.mp4"];
          
          [[NSFileManager defaultManager]moveItemAtPath:[location path ]toPath:filepath error:Nil];
          
          NSLog(@"%@",filepath);
      }
      
    }];
    
    [task resume];
}

 

利用NSURLSession在网络里下载视频

标签:

原文地址:http://www.cnblogs.com/appshan/p/4569308.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!