利用NSURLSession在网络里下载视频

- (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];
}
原文地址:https://www.cnblogs.com/appshan/p/4569308.html