首页 > Web开发 > 详细

利用NSURLSession在网络里下载视频

时间:2015-06-11 16:48:56      阅读:205      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!