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 ([[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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章